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

What is the purpose of system calls, Q. What is the purpose of system calls...

Q. What is the purpose of system calls? Answer: System calls permit user-level processes to request services of the operating system.

Unix script, As a group, you may need to make some enhancements to your dev...

As a group, you may need to make some enhancements to your development server (NOT your stable server!)  It needs to be able to run the makepasswd program, which may require instal

What are the various available scheme in ipc, Several schemes available are...

Several schemes available are as follows: Pipes : One-way communication process through which different process can interact. The problem is that the two processes could have

OS is a resource allocator and control program, why we say OS is a resource...

why we say OS is a resource allocator and control program

What are the different accessing methods of a file, What are the different ...

What are the different accessing methods of a file? The different types of accessing a file are: Sequential access: Information in the file is accessed sequentially

Memory management, what is segmentation as it refers to operating system

what is segmentation as it refers to operating system

Data transfer between two processes, Q. Data transfer between two processes...

Q. Data transfer between two processes? Communication: Data transfer between two processes is essential for some time. Both processes are on the one computer or on different

Operating system as user interface, Q. Operating System as User Interface? ...

Q. Operating System as User Interface? • Every general purpose computer comprises the operating system, hardware, system programs and application programs. The hardware compris

Determine what the blocking factor of a file is, Determine what the “blocki...

Determine what the “blocking factor” of a file is Blocking factor is the number of logical records in one physical record

Relationship among a(x, Q. The RC 4000 system as well as other systems hav...

Q. The RC 4000 system as well as other systems have defined a tree of processes (called a process tree) such that all the descendants of a process are given resources (objects) as

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