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
Lexical substitution during macro expansion Lexical substitution is employed to produce an assembly statement from a model statement. A model statement contains 3 types of stri

Q. What are the five main activities of an operating system in regard to process management? Answer: a. The creation as well as deletion of both user and system processes

A paper mill produces two grades of paper viz., X and Y. Because of raw material restrictions, it cannot produce more than 400 tons of grade X paper and 300 tons of grade Y paper i

Parent and Child process communicate in unix A child and parent can interact through any of the normal  inter-process communication schemes (pipes, message queues, sockets, s

How can a user program disturb the normal operation of the system? a) Issuing illegal I/O operation. b) By accessing memory locations within the OS itself. c) Refusing to

Explain about threading issues? The fork and exec system calls In a multithreaded program of few UNIX systems have chosen to have two versions of fork, one that duplicates e

Thermostats are subjected to rigorous testing before they are shipped to air conditioning technicians around the world. Results from the last five samples are shown in the table. D

Explain working of the logical file system The logical file system manages metadata information. Metadata contains all of the file-system structure, excluding actual data. It h

Explain the ThreadSwitchWithDelay Function used in Netware ThreadSwitchWithDelay( )   This command is used when threads are waiting for an event or resource but don't want t

Q. How could a system be designed to allow a choice of operating systems to boot from? What would the bootstrap program need to do? Answer: Delieve a system that would like to