创建子进程

/* This program creates a new process and allows both child and parent

to report their idenfication numbers. */

 

#include <sys/types.h> /* file of data types needed for many compilers */

#include <unistd.h> /* needed for fork, getpid procedures */

#include <stdio.h>

 

int main (void)

{

pid_t pid; /* variable to record process id of child */

 

pid = fork(); /* create new process */

if ( -1 == pid) /* check for error in spawning child process */

{ perror ("error in fork");

exit (1);

{

 

if (0 == pid) /* check if this is the new child process */

{ /* processing for child */

printf ("This output comes from the child process/n");

printf ("Child report: my pid = %d/n", getpid());

{

else

{ /* processing for parent */

printf ("This output comes from the parent process./n");

printf ("Parent report: my pid = %d child's pid = %d/n", getpid(), pid);

{

 

exit (0); /* quit by reporting no error */

}

 

 

 

Process control procedures typically use the #include <unistd.h> library. On many

systems, this also requires the #include <sys/types.h> library.

 

Each process has a process identi cation number, which has type pid_t.

The fork (pid_t fork(void)) procedure spawns a new process, copying exactly the elements of the current

process. The new process is called a child process, and the previously existing process is

called the parent.

 

That is, the newly created process gets a copy of the parent's data speace, heap, and stack.

Open file attributes are copied.

 

Both the parent and child continue execution at the point following the call to fork.

The behavior of the two processes is identical, except for the value returned by fork:

fork returns the process id of the child to the parent, assuming the fork was successful.

(If a new process cannot be created, fork returns 1 to the parent.)

fork returns 0 to the child process.

 

while the get (pid_t getpid(void)) returns the process ID of the current process. (This is often used by routines that generate unique temporary filenames.)

 

 

After the fork operation, the two processes execute independently.

Since process operations can fail, sometimes leaving administrative and bookkeeping details behind, programs always should test such calls for errors and provide an appropriate

and graceful exit if errors have occured. This is commonly done in two steps:

perror() writes an error message on standard error, based on an internal error

coding variable -errno.

exit(1)  causes the current process to terminate and returns an integer variable

(status) to the operating system for further inspection. Usually, exit(0) indicates

no error has occured, while a nonzero value (e.g., exit(1);) indicates some error

has occurred.

Following error checking, the two processes (parent and child) usually go their separate

ways. This is accomplished by testing the value of the returned process id.

Any process may obtain its own process id with the function getpid(). Similarly, a

process may obtain the process id of its parent with getppid().

 

Once the program runs on, you can find the pid by ps aux in shell and kill them by pid. This is useful when your OS becomes slow due to some certain processes which consume the CPU. At that time, top command will sort the processes according to their consumption of CPU.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值