![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Wrapping Your Brain Around Oracle + Python | iBot | Oracle Updates (RSS) | 0 | 04-06-2008 02:10 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
C Brain Teaser
Dear Gurus,
I have encountered a C question, which I thought of sharing with you. This question was asked by one of my technical training staff...Though my training was over I'm still thinking of a solution for this.. Write a C program to do a small task(lets say just simply printing a "Hello World" or so) such that the program should only give output or the desired result ony once after it got compiled and should never give the output nor do anything else. The program should not make use of files or any extrnal means of storing some data. Please suggest me some ways of achieving this... Regards RK |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Quote:
Code:
[/tmp]$ cat try.cpp
#include <stdio.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
printf ("Bye Bye Cruel World :-( \n");
return (unlink (argv[0]));
}
[/tmp]$ gcc -o try try.cpp -lstdc++
[/tmp]$ ls -l try
-rwxr-xr-x 1 xxxxxx g900 5202 May 9 02:51 try
[/tmp]$ ./try
Bye Bye Cruel World :-(
[/tmp]$ ls -l try
ls: try: No such file or directory
[/tmp]$
|
|
#3
|
||||
|
||||
|
Astounding that that works, vino! Looking at the Linux man page for unlink(2), I see that it does not detect "text busy". Compare that to the HP-UX man page for unlink(2). The Linux unlink(2) is rather permissive. If that man page is right, it even allows unlinking an active mount point! I'm not sure what effect that would have... Posix does not require that unlink(2) disallow unlinking mount points (but the text in the standard makes it clear that this is because Posix does not ever mention mount points). But unlinking an active program is another matter. On one hand, the Posix Standard says:
Quote:
Quote:
A more portable solution would be have the program to write a little shell script to unlink the file and then exec that shell script. |
|
#4
|
||||
|
||||
|
I just tested on Solaris 10, t1000 server, seems to be working :
Quote:
|
|
#5
|
|||
|
|||
|
Does not work on HPUX 11i, as expected.
|
|
#6
|
|||
|
|||
|
it works on RHEL3 !
|
|
#7
|
||||
|
||||
|
Quote:
|
||||
| Google The UNIX and Linux Forums |
| Tags |
| linux |
| Thread Tools | |
| Display Modes | |
|
|