多进程编程

1:创建一对父子进程 父进程负责:打开文件,向文件中写入键盘键入的数据 子进程负责:打开相同的文件,从文件中读取刚才键入的数据 注意:想办法保证先写入数据,后读取数据

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
	pid_t pid;
    char buf[128]={0};
    int res,wes;

    // 打开(或创建)文件
   int fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);

   	pid=fork();//创建子程序
    if (pid == 0) 
	{
        close(fd); // 关闭子进程中的文件描述符,因为我们将在读取时重新打开
        sleep(10); // 等待父进程写入数据

        // 重新打开文件以读取

        // 读取并打印文件内容
        fd=open("./test.txt",O_RDONLY);
        res = read(fd, buf, 127);
        if (res == 0) {
			printf("文件为空");
		}
		else{
			printf("子进程要读的是: %s\n", buf);
		}
        close(fd);
    } else {
        // 父进程
        // 等待用户输入并写入文件
        printf("用户输入: ");
        fgets(buf, 128, stdin);

        // 移除换行符(如果存在)
        int len = strlen(buf);
        if (len > 0 && buf[len - 1] == '\n') {
            buf[len - 1] = '\0';
        }

        wes = write(fd, buf, strlen(buf));
        if (wes == -1) {
            perror("写入失败");
            close(fd);
        }

        close(fd); // 关闭文件描述符

        // 等待子进程结束
        waitpid(-1,0,0);

        printf("父写入进程结束\n");
    }
	return 0;
}

2:编写2个进程 进程1:负责使用execl 替换成 shell指令 find /usr -name include 进程2:负责使用execv 替换成 shell指令 find /usr -name include

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
    pid_t pid=fork();

    char *argv[2] = {"demo", NULL}; 

    pid = fork();

    if (pid == -1) {
        perror("fork failed");
        exit(EXIT_FAILURE);
    } else if (pid == 0) {
        // 子进程
        execv("./demo", argv); // 使用execv通过shell执行命令


    } else {
        // 父进程
        execl("./demo","demo", NULL); 

        wait(NULL); // 等待子进程结束
    }
	return 0;
}



int main{
syste("find /usr -name include");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值