进程创建
1. 在Linux下练习使用fork()创建进程
- 如果父进程要等待子进程结束再进行;
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main(){
pid_t pid;
pid = fork();
if (pid < 0){
printf("fork failed!\n");
exit(-1);
}
else if (pid == 0){
printf("Child Begin\nChildPid:%d\n",getpid());
}
else{
wait(NULL);
printf("Child Complete\n");
printf("Parent Begin\nParentPid:%d\n",getpid());
printf("Parent Complete\n");
exit(0);
}
}
2)如果父进程不等待子进程。注释wait
结论:如果父进程要等待子进程,父进程调用wait函数后阻塞,当子进程结束后,系统给父进程发送sigchild信号来唤醒父进程,然后父进程执行
如果父进程不等待子进程,父进程会先执行完毕后,再开始执行子进程
2.创建子进程
满足下列要求:
- 先让子进程输出当前所在路径
- 再让父进程在当前目录下新建一个名为hise的文件夹
- 输出子进程和父进程的pid号
- 最后让父进程将本程序源码文件(.c)拷贝到新建的hise文件夹
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/wait.h>
int main(){
pid_t pid = fork();
int fd[2];
int fd_file = open("./lab2.c",O_RDONLY);
if( pid < 0) {
exit(-1);
}
else if(pid == 0) {
char path1[1024] = {0};
getcwd(path1, sizeof(path1));
printf("child path1 = %s\n", path1);
write(fd[1], path1, strlen(path1));
printf("child PID = %d, parent PID = %d\n", getpid(), getppid());
}
else {
wait(NULL);
char path2[1024] = {0};
char buf[1024];
fcntl(fd[0], F_SETFL);
read(fd[0], buf, sizeof(buf));
if(mkdir("hise", 0755)) {
perror("mkdir");
exit(1);
}
chdir("hise");
int fd_file2 = open("./cplab2.c",O_WRONLY | O_CREAT, 0644);
char buf2[1024];
memset(buf2, 0, 1024);
while(read(fd_file, buf2, sizeof(buf2)-1) > 0 ) {
write(fd_file2, buf2, strlen(buf2));
}
}
return 0;
}
3.一个父进程创建多个子进程
pid_t pid1, pid2;
pid1=fork();
Int counter = 0;
if(pid1==0){
counter = counter + 1;
printf("%d %d\n",counter,getpid());
}
pid2=fork();
if(pid2==0) {
counter = counter + 2;
printf("%d %d\n",counter,getpid());
}
else {
waitpid(pid1,NULL,0);
waitpid(pid2,NULL,0);
counter = counter + 100;
printf("%d %d\n",counter,getpid());
}
发现无法运行,输出结果说明文件中无循环结构但输出了五个数据,说明了其中子进程又创建了子进程
改成如下即可
#将其下方一组if-else放到一个else中即可
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
int main(){
pid_t pid1, pid2;
pid1=fork();
int counter = 0;
if(pid1==0) {
counter = counter + 1;
printf("%d %d\n",counter,getpid());
}
else{
pid2=fork();
if(pid2==0) {
counter = counter + 2;
printf("%d %d\n",counter,getpid());
}
else{
waitpid(pid1,NULL,0);
waitpid(pid2,NULL,0);
counter = counter + 100;
printf("%d %d\n",counter,getpid());
}
}
}
进程间通信
#producer
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
int main(){
const int SIZE = 4096;
const char *name = "OS";
const char *message_0 = "Hello";
const char *message_1 = "World!";
int shm_fd;
void *ptr;
shm_fd = shm_open(name,O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd,SIZE);
ptr = mmap(0,SIZE,PROT_WRITE,MAP_SHARED,shm_fd,0);
sprintf(ptr,"%s",message_0);
ptr+=strlen(message_0);
sprintf(ptr,"%s",message_1);
ptr+=strlen(message_1);
return 0;
}
~
#consumer
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
int main(){
const int SIZE = 4096;
const char *name ="OS";
int shm_fd;
void *ptr;
shm_fd = shm_open(name,O_RDONLY,0666);
ptr = mmap(0,SIZE,PROT_READ,MAP_SHARED,shm_fd,0);
printf("%s",(char *)ptr);
shm_unlink(name);
return 0;
}
实验结果说明,POSIX常用于线程;system v常用于进程的同步
POSIX 在无竞争条件下,不需要陷入内核,执行系统调用,性能占用小
System V 无论有无竞争都要执行系统调用,性能占用高