0
点赞
收藏
分享

微信扫一扫

和菜鸟一起深入学习国嵌实验之进程创建,exec函数,进程等待


1、 fork进程创建

代码:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>

int main(int argc, char *argv[])
{
pid_t child;

//create child process
if((child = fork()) == -1)
{
printf("Fork Error: %s\n", strerror(errno));
exit(1);
}
else
{
if(child == 0)
{
printf("I am the child : %d\n", getpid());
exit(0);
}
else
{
printf("I am the father : %d\n", getpid());
return 0;
}
}

return 0;
}



 

Makefile:


CC = gcc

CURTDIR = $(shell pwd)
TARGET = myfork

%.o:%.c
$(CC)-c $(EXTRAFLAGS) $< -o $@
%.o:%.S
$(CC)-c $(EXTRAFLAGS) $< -o $@

.PHONY: all clean

$(TARGET): $(TARGET).o
$(CC) -o $@ $^

clean:
rm-rf $(TARGET) $(TARGET).o



 

 

运行结果:


eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.8$make
gcc -c myfork.c -o myfork.o
gcc -o myfork myfork.o

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.8$ls
Makefile myfork myfork.c myfork.o

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.8$./myfork
I am the father : 8694
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.8$I am the child : 8695



总结:fork函数创建子进程后,父子进程是独立、同时运行的,并没有先后顺序之分。

 

 

 

2、vfork进程创建

代码:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>

int main(int argc, char *argv[])
{
pid_t child;

//create child process
if((child = vfork()) == -1)
{
printf("Fork Error: %s\n", strerror(errno));
exit(1);
}
else
{
if(child == 0)
{
sleep(1);
printf("I am the child :%d\n", getpid());
exit(0);
}
else
{
printf("I am the father : %d\n", getpid());
return 0;
}
}

return 0;
}



 

Makefile:


CC = gcc

CURTDIR = $(shell pwd)
TARGET = myvfork

%.o:%.c
$(CC)-c $(EXTRAFLAGS) $< -o $@
%.o:%.S
$(CC)-c $(EXTRAFLAGS) $< -o $@

.PHONY: all clean

$(TARGET): $(TARGET).o
$(CC) -o $@ $^

clean:
rm-rf $(TARGET) $(TARGET).o



 

运行结果:


eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.9$make
gcc -c myvfork.c -o myvfork.o
gcc -o myvfork myvfork.o

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.9$ls
Makefile myvfork myvfork.c myvfork.o

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.9$./myvfork
I am the child : 8870
I am the father : 8869



 

 

总结:调用fork函数产生的父子进程的运行顺序是不定的,而调用vfork函数产生的父子进程必定是子进程先运行完,父进程再运行的。

 

 

3、 exec函数族

代码:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>

int main(int argc, char *argv[])
{
if(argc < 2)
{
perror("you haven't input the filename, please try again!\n");
exit(EXIT_FAILURE);
}

if(execl("./file_create", "file_creat", argv[1],NULL) < 0)
perror("execl error!\n");

return 0;
}



 

Makefile:


CC = gcc

CURTDIR = $(shell pwd)
TARGET = myexec

%.o:%.c
$(CC)-c $(EXTRAFLAGS) $< -o $@
%.o:%.S
$(CC)-c $(EXTRAFLAGS) $< -o $@

.PHONY: all clean

$(TARGET): $(TARGET).o
$(CC) -o $@ $^

clean:
rm-rf $(TARGET) $(TARGET).o



 

 

运行结果:


eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.10$make
gcc -c myexec.c -o myexec.o
gcc -o myexec myexec.o

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.10$ls
file_create Makefile myexec myexec.c myexec.o


eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.10$./myexec file
create file file success!

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.10$ls
file file_create Makefile myexec myexec.c myexec.o



 

 

总结:exec函数族会在一个进程中启动另一个进程执行。并用它来取代原调用进程的数据段、代码段和堆栈段。在执行完exec函数调用后,原调用进程的内容除了进程号之外,其他全部都被新的进程替换了。

 

4、 等待进程

代码:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>

int main(int argc, char *argv[])
{
pid_t child;

//create child process
if((child = vfork()) == -1)
{
printf("Fork Error: %s\n", strerror(errno));
exit(1);
}
else
{
if(child == 0)
{
printf("the child process is run\n");
sleep(1);
printf("I am the child : %d\n", getpid());
exit(0);
}
else
{
wait(NULL);
printf("the father process is run\n");
printf("I am the father : %d\n", getpid());
return 0;
}
}

return 0;
}



 

Makefile:


CC = gcc

CURTDIR = $(shell pwd)
TARGET = mywait

%.o:%.c
$(CC)-c $(EXTRAFLAGS) $< -o $@
%.o:%.S
$(CC)-c $(EXTRAFLAGS) $< -o $@

.PHONY: all clean

$(TARGET): $(TARGET).o
$(CC) -o $@ $^

clean:
rm-rf $(TARGET) $(TARGET).o



 

运行结果:


eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.11$make
gcc -c mywait.c -o mywait.o
gcc -o mywait mywait.o

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.11$ls
Makefile mywait mywait.c mywait.o

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.11$./mywait
the child process is run
I am the child : 9275
the father process is run
I am the father : 9274



 

 

结论:用fork创建子进程后,父子进程的执行顺序fork函数不能确定。我们可以用wait函数和waitpid函数使父进程阻塞等待子进程退出的性质,来确保子进程先结束。

 

举报

相关推荐

0 条评论