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
Define Segmentation  The memory allocation method subject to “external” fragmentation is Segmentation.

What is external fragmentation? As process are removed from and loaded to the memory free memory space is bracken into pieces .external fragmentation take place when enough mem

Question : a) Describe the difference between the authentication of a user account on a peer to peer network and a client server network. b) Compare an end user account with

SCAN o Go from the outside to the inside servicing requests and after that back from the outside to the inside servicing requests. o Replicate this over and over. o Diminish

Discuss in detail Table management Techniques?     An Assembler employs the subsequent tables: OPTAB: Operation Code Table consists of mnemonic operation code and its machi

How is memory management done using linked lists?   Each one node of the l list will have 4 fields as follows 1-this tells whether it s a hole or a process 2-starting ad

Which are the different states of a process?   Different status of a process is: New: When the process is created. Running: When instructions are executed. Wait

Explain Rehashing to Handle Collision Rehashing:  Re-hashing schemes make use of a second hashing operation while there is a collision. If there is an additional collision, we

What is the information associated with an open file? Various pieces of information are associated with  an open file which may be: File pointer Disk location of th

Android operating system is an open source, Linux-based Smartphone OS. It is the world's most popular mobile platform with more than 700,000 applications and 25 billion downlo