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
protection in operating system

The Cigarette-Smokers difficulty Consider a system with three smoker processes and one agent process. Every smoker continuously rolls a cigarette and then smokes it. But to roll as

Clustered file organization A clustered file is also called a B-tree-clustered file is a tree-structured file in which records with adjacent index values are clustered toget

How is process management done in multiprocessing systems ? There are two accost for the process management in multiprocessor systems. Master-slave configuration Sy

The field that consists of a segment index or an internal index is known as Target datum is field that consists of a segment index or an internal index.

How a virtual memory can be implemented? What are the benefits of using virtual memory?

Question: a) The following questions refers to Windows XP networking: i) Briefly, explain how a host joins a network using DHCP? ii) Which IP address could be assigned to a

Question 1 Explain single Partition Allocation and Multiple Partition Question 2 What is PCB? What useful information is available in PCB? Question 3 Explain Preemptive and No

We have stressed the need for an operating system to make efficient use of the computing hardware. When is it appropriate for the operating system to forsake this principle and to

Q) a. Given that the first three necessary conditions for a deadlock are in place, comment on the feasibility of the following strategy. All processes are givenĀ  unique priorities.