Unix process api, Operating System

Assignment Help:

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.


Related Discussions:- Unix process api

Pages into physical memory, For reading, most operating systems use demand ...

For reading, most operating systems use demand paging. This means that pages are only read from the disk into physical memory when they are needed. In the page table, there is a re

What is virtual file system?, What is virtual file system? A Virtual f...

What is virtual file system? A Virtual file system switch (VFS) or Virtual file system is an abstraction layer   on top of a more concrete file system. The purpose of a VFS

Difference between system calls and procedure calls, The Most comman differ...

The Most comman difference are given below System calls are heavy. While a procedure call can generally be performed in a few system instructions, a system call needs the c

Explain direct sequence-analog signal, 1. A digitized voice channel is made...

1. A digitized voice channel is made by digitizing a 7 KHz bandwidth analog signal. The signal is to be sampled twice the highest frequency (two samples per hertz) . Assume each sa

List the four steps that are essential to run a program, List the four step...

List the four steps that are essential to run a program on a completely dedicated machine. a. Reserve machine time. b. Manually load program into memory. c. Load starting

State critical section problem, State critical section problem? Discuss thr...

State critical section problem? Discuss three solutions to solve the critical section problem. C-S Problem:- n processes all competing to use some shared data Every

Concepts of an address - data and control bus, Let us consider the operatio...

Let us consider the operation of the EPROM device in more detail. Consider the pining details below again   Before we examine the interface means of the EPROM, it is worth

Evaluate the physical address, Compare between the one and two-dimensional ...

Compare between the one and two-dimensional memory organizations in terms of the  memory structure,  advantages, and disadvantages.  Which approach would better support the needs o

Explain the worker model, Explain the Worker Model The Worker Model of ...

Explain the Worker Model The Worker Model of client-server application architecture provides a very good understanding of threads and their power to the developer. This exercis

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd