Unix process api, Operating System

Unix process API

The two most important function calls to use when programming with several processes are fork and exec:

 fork() creates a copy of current process. It gives a different return value to each process and works based on Copy On Write;

 exec() replaces a process with an executable.

(The Windows CreateProcess(...), taking ten arguments, is analogous.)

Notice that fork() implies that each process descends from another process. In fact, in Unix everything descends from a single process called init: basically, init forks a process and then "replaces its code" with, say, the code of bash, using exec().

Example of how to use fork:
#include
#include
#include
int parentid = getpid();
char program_name[1024];
gets(program_name); // reads the name of program we want to start
int cid = fork();
if (cid==0) { // i'm the child
execlp(program_name, program_name, 0); // loads the program and runs it
printf("if the above worked, this line will never be reached\n");
}
else { // i'm the parent
sleep (1); // give my child time to start
waitpid(cid, 0, 0); // waits for my child to terminate
print("program %s finished\n", program_name);
}
Is the sleep(1) call necessary to allow the child process to start? The answer is no, it is not at all necessary. In general, if you think you need to sleep in a program, you are probably doing something wrong, and just slowing down your program. The call to waitpid() is a blocking wait, and will ?rst wait to let the child process start (if it hasn't already), then will wait until it ends.

Posted Date: 3/12/2013 4:12:42 AM | Location : United States







Related Discussions:- Unix process api, Assignment Help, Ask Question on Unix process api, Get Answer, Expert's Help, Unix process api Discussions

Write discussion on Unix process api
Your posts are moderated
Related Questions
What are interrupts? Interrupts are in some ways the mainly "famous" system resources, ever since almost everyone who's used a computer has heard of them even if they don't k

Q. Consider a file at present consisting of 100 blocks. Presume that the file control block (and the index block in the case of indexed allocation) is already in memory. Compute h

What is the use of fork and exec system calls? Fork is a system call by which a latest process is created. Exec is also a system call, which is used after a fork by one of the

What is meant by arm-stickiness? If one or a few processes have a high access rate to data on single track of a storage disk, then they might be monopolizing the device by repe

NEXT FIT ALGORITHM Here scanning starts from the first fit position and then it finds the next position which is large sufficient to hold the process. Thus the name next fit.

Refer a program with a linked origin of 5000. Suppose the memory area allocated to it have the start address of 70000. Determine the value to be loaded in relocation register?

What are the requirements for solution of critical section problems? Mutual exclusion : If process p is implementing in its critical section then no other processes can be exe


Briefly discuss on Bankers algorithm? The resource-allocation graph algorithm is not applicable to a resource-allocation system with multiple examples  for each resource type.

Q. Is the Internet a LAN or else a WAN? Answer: The Internet is a WAN as the a variety of computers are located at geographically different places and are connected by long-dis