linux fork创建新进程

man 2 fork

使用fork创建和当前进程一模一样的进程,叫子进程原来的进程叫父进程

pid_t fork(void);

无输入参数,执行成功后返回子进程pid给父进程,返回0 给子进程,当内存不够或者pid号用尽的时候执行失败,此时返回-1

一些提示点(来自man):

  *  The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)) or session.
  *  The child's parent process ID is the same as  the  parent's  process ID.
  *  The  child  does  not  inherit  its parent's memory locks (mlock(2), mlockall(2)).
  *  Process resource utilizations (getrusage(2)) and CPU  time  counters(times(2)) are reset to zero in the child.
  *  The  child's  set  of  pending  signals is initially empty (sigpend‐ ing(2)).
  *  The child does not inherit semaphore  adjustments  from  its  parent(semop(2)).
  *  The  child does not inherit process-associated record locks from its parent (fcntl(2)).  (On the other hand,  it  does  inherit  fcntl(2)  open file description locks and flock(2) locks from its parent.)
  *  The  child  does  not  inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)).
  *  The child does not inherit outstanding asynchronous  I/O  operations from its parent (aio_read(3), aio_write(3)), nor does it inherit any asynchronous I/O contexts from its parent (see io_setup(2)).
     The process attributes in the  preceding  list  are  all  specified  in  POSIX.1.   The parent and child also differ with respect to the follow‐ing Linux-specific process attributes:
  *  The child does not inherit directory change notifications  (dnotify) from its parent (see the description of F_NOTIFY in fcntl(2)).
  *  The  prctl(2)  PR_SET_PDEATHSIG  setting  is reset so that the child
     does not receive a signal when its parent terminates.

测试用例:

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
    pid_t pid;
    int i=100;
    pid =fork();
    //fork error
    if(pid==-1)
    {
        printf("fork error \n");
        return 1;
    }
    else if(pid)// this is parent process
    {
        i=200;
        printf ("i in parent process is %d\n",i);
    }
    else if(pid==0)//this is child process
    {
        i=300;
        printf ("i in child process is %d\n",i);
    }
    
    return 0;
}

综合测试代码:fork,exec balabala

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
	char *arg[] = {"ls","-a",NULL};
	
	if(fork() == 0){
		//in child1
		printf("fork1 is OK;execl\n");
		
		if(execl("/bin/ls","ls","-a",NULL) == -1){
			perror("execl error");
			exit(1);
		}
	}

	usleep(20000);
	if(fork() == 0){
		//in child2
		printf("fork2 is OK;execv\n");
		
		if(execv("/bin/ls",arg) == -1){
			perror("execv error");
			exit(1);
		}
	}
	
	usleep(20000);
	if(fork() == 0){
		//in child3
		printf("fork3 is OK;execlp\n");
		
		if(execlp("ls","ls","-a",NULL) == -1){
			perror("execlp error");
			exit(1);
		}
	}
	
	usleep(20000);
	if(fork() == 0){
		//in child4
		printf("fork4 is OK;execvp\n");
		
		if(execvp("ls",arg) == -1){
			perror("execvp error");
			exit(1);
		}
	}
	
	usleep(20000);
	if(fork() == 0){
		//in child5
		printf("fork5 is OK;execle\n");
		
		if(execle("/bin/ls","ls","-a",NULL,NULL) == -1){
			perror("execle error");
			exit(1);
		}
	}
	
	usleep(20000);
	if(fork() == 0){
		//in child6
		printf("fork6 is OK;execve\n");
		
		if(execve("/bin/ls",arg,NULL) == -1){
			perror("execve error");
			exit(1);
		}
	}
	//加入小延时可以避免发生混乱的情况
	usleep(20000);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

T触发器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值