第二十四次作业

本文详细介绍了如何使用C语言通过管道和信号灯机制实现两个进程之间的并发通信,包括主进程发送和接收消息,以及子进程的相应操作,同时利用互斥锁保证数据同步。
摘要由CSDN通过智能技术生成
  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <unistd.h>
  4 #include <pthread.h>
  5 #include <string.h>
  6 #include <fcntl.h>
  7 #include <sys/stat.h>
  8 #include <errno.h>
  9 
 10 struct h
 11 {
 12     int fp;
 13     int fp1;
 14     pthread_mutex_t mutex;
 15     pthread_t jing1;
 16     pthread_t jing2;
 17 };
 18 
 19 void* zifa(void* hh)
 20 {
 21     struct h* uu = (struct h*)hh;
 22     while(1)
 23     {
 24         pthread_mutex_lock(&uu->mutex);
 25         char message[128];
 26         printf("主发>>");
 27         bzero(message, sizeof(message));
 28         fgets(message, sizeof(message), stdin);
 29         message[strlen(message)-1]='\0';
 30         write(uu->fp1, message, sizeof(message));
 31         if (strcmp(message, "quit") == 0)
 32         {
 33             break;
 34         }                                                                                 
 35         pthread_mutex_unlock(&uu->mutex);
 36     }
 37     pthread_exit(NULL);
 38 }
 39 
 40 void* zshou(void* bb)
 41 {
 42     struct h* uu = (struct h*)bb;
 43     while(1)
 44     {
 45         char message[128];
 46         bzero(message, sizeof(message));
 47         read(uu->fp, message, sizeof(message));
 48         if (strcmp(message, "quit") == 0)
 49        {
 50             write(uu->fp1,message,sizeof(message));
 51             close(uu->fp);
 52             break;
 53         }
 54         printf("主收到:%s\n", message);
 55     }
 56     pthread_exit(NULL);
 57 }
 58 
 59 int main(int argc, const char *argv[])
 60 {
 61     struct h uu;
 62 
 63     if(mkfifo("./fifo1", 0777) < 0)
 64     {
 65         if(errno != EEXIST)
 66         {
 67             perror("mkfifo");
 68             return -1;
 69         }
 70     }
 71 
 72     if(mkfifo("./fifo2", 0777) < 0)
 73     {
 74         if(errno != EEXIST)
 75         {
 76             perror("mkfifo");
 77             return -1;
 78         }
 79     }
 80 
 81     int fp = open("./fifo1", O_RDWR);
 82     int fp1 = open("./fifo2", O_RDWR);
 83 
 84     uu.fp = fp;
 85     uu.fp1 = fp1;
 86 
 87     if(fp < 0 || fp1 < 0)
 88     {
 89         printf("管道文件打开失败\n");
 90         return -1;
 91     }
 92 
 93     pthread_mutex_init(&uu.mutex, NULL);
 94 
 95     pthread_t jing1, jing2;
 96 
 97     if(pthread_create(&jing1, NULL, zifa, (void*)&uu) != 0)
 98     {
 99         printf("主发失败\n");
100         return -1;
101     }
102 
103     if(pthread_create(&jing2, NULL, zshou, (void*)&uu) != 0)
104     {
105         printf("主收失败\n");
106         return -1;
107     }
108     uu.jing1=jing1;
109     uu.jing2=jing2;
110     pthread_join(jing1, NULL);
111     pthread_join(jing2, NULL);
112 
113     close(fp);
114     close(fp1);
115 
116     pthread_mutex_destroy(&uu.mutex);
117 
118     return 0;
119 }
120 
~                                                                                             
~                                                                                   
  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <unistd.h>
  4 #include <pthread.h>
  5 #include <string.h>
  6 #include <fcntl.h>
  7 #include <sys/stat.h>
  8 #include <errno.h>
  9 
 10 struct h
 11 {
 12     int fp;
 13     int fp1;
 14     pthread_mutex_t mutex;
 15 };
 16 
 17 void* zifa(void* hh)
 18 {
 19     struct h* uu = (struct h*)hh;
 20     while(1)
 21     {
 22         pthread_mutex_lock(&uu->mutex);
 23         char mes[128];
 24         printf("子发>>");
 25         bzero(mes, sizeof(mes));
 26         fgets(mes, sizeof(mes), stdin);
 27         mes[strlen(mes)-1]='\0';
 28         write(uu->fp,mes , sizeof(mes));
 29                                                                                           
 30         if (strcmp(mes, "quit") == 0)
 31         {
 32             break;
 33         }
 34         pthread_mutex_unlock(&uu->mutex);
 35     }
 36     pthread_exit(NULL);
 37 }
 38 
 39 void* zshou(void* bb)
 40 {
 41     struct h* uu = (struct h*)bb;
 42     while(1)
 43     {
 44         char temp[128];
 45         bzero(temp, sizeof(temp));
 46         read(uu->fp1, temp, sizeof(temp));
 47         if (strcmp(temp, "quit") == 0)
 48         {
 49             write(uu->fp,temp,sizeof(temp));
 50             close(uu->fp1);
 51             break;
 52         }
 53         printf("子收到:%s\n", temp);
 54     }
 55     pthread_exit(NULL);
 56 }
 57 
 58 int main(int argc, const char *argv[])
 59 {
 60     struct h uu;
 61 
 62     if(mkfifo("./fifo1", 0777) < 0)
 63     {
 64         if(errno != EEXIST)
 65         {
 66             perror("mkfifo");
 67             return -1;
 68         }
 69     }
 70 
 71     if(mkfifo("./fifo2", 0777) < 0)
 72     {
 73         if(errno != EEXIST)
 74         {
 75             perror("mkfifo");
 76             return -1;
 77         }
 78     }
 79 
 80     int fp = open("./fifo1", O_RDWR);
 81     int fp1 = open("./fifo2", O_RDWR);
 82 
 83     uu.fp = fp;
 84     uu.fp1 = fp1;
 85 
 86     if(fp < 0 || fp1 < 0)
 87     {
 88         printf("管道文件打开失败\n");
 89         return -1;
 90     }
 91 
 92     pthread_mutex_init(&uu.mutex, NULL);
 93 
 94     pthread_t jing1, jing2;
 95 
 96     if(pthread_create(&jing1, NULL, zifa, (void*)&uu) != 0)
 97     {
 98         printf("子发失败\n");
 99         return -1;
100     }
101 
102     if(pthread_create(&jing2, NULL, zshou, (void*)&uu) != 0)
103     {
104         printf("子收失败\n");
105         return -1;
106     }
107 
108     pthread_join(jing1, NULL);
109     pthread_join(jing2, NULL);
110 
111     close(fp);
112     close(fp1);
113 
114     pthread_mutex_destroy(&uu.mutex);
115 
116     return 0;
117 }
                                                                                     

运行效果;

信号灯集实现打印效果,代码

  5 #include <stdlib.h>
  6 #include <unistd.h>
  7 #include <sys/sem.h>
  8 #include <string.h>
  9 
 10 int main(int argc, const char *argv[])
 11 {
 12     key_t key = ftok("/home/ubuntu/", 8);
 13     if (key < 0)
 14     {
 15         perror("ftok");
 16         return -1;
 17     }
 18 
 19     int sem = semget(key, 2, IPC_CREAT | 0777);
 20     if (sem < 0)
 21     {
 22         perror("semget");
 23         return -1;
 24     }
 25 
 26     unsigned short arr[2] = {1, 0}; // 初始化第一个信号量为1,第二个信号量为0
 27     if (semctl(sem, 0, SETALL, arr) < 0)
 28     {
 29         perror("semctl");
 30         return -1;
 31     }
 32 
 33     int mid = shmget(key, 128, IPC_CREAT | 0777);
 34     if (mid < 0)
 35     {
 36         perror("shmget");
 37         return -1;
 38     }
 39 
 40     void *shemp = shmat(mid, NULL, 0);
 41     if ((void *)-1 == shemp)
 42     {
 43         perror("shmat");
 44         return -1;
 45     }
 46 
 47     char *temp = (char *)shemp;
 48     strcpy(temp, "1234567");
 49 
 50     struct sembuf pr;
 51 
 52     while (1)
 53     {
 54         pr.sem_num = 0;
 55         pr.sem_op = -1;
 56         pr.sem_flg = 0;                                                                                                                                                                                                                                                                                    
 57         if (semop(sem, &pr, 1) < 0)
 64         {
 65             perror("semo");
 66             return -1;
 67        
 58         printf("%s\n", temp);
 59 
 60         pr.sem_num = 1;
 61         pr.sem_op = +1;
 62         pr.sem_flg = 0;
 63         if (semop(sem, &pr, 1) < 0)
 64         {
 65             perror("semop");
 66             return -1;
 67         }
 68     }
 69 
 70     semctl(sem, 0, IPC_RMID);
 71     shmctl(mid, IPC_RMID, NULL);
 72 
 73     return 0;
 74
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/sem.h>
#include <string.h>

int main(int argc, const char *argv[])
{
    key_t key = ftok("/home/ubuntu/", 8);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }

    int sem = semget(key, 2, IPC_CREAT | 0777);
    if (sem < 0)
    {
        perror("semget");
        return -1;
    }

    int mid = shmget(key, 128, IPC_CREAT | 0777);
    if (mid < 0)
    {
        perror("shmget");
        return -1;
    }

    void *shemp = shmat(mid, NULL, 0);
    if ((void *)-1 == shemp)
    {
        perror("shmat");
        return -1;
    }

    char *temp = (char *)shemp;
    strcpy(temp, "1234567");

    struct sembuf pr;

    while (1)
    {
        pr.sem_num = 1; // 第一个信号量P操作
        pr.sem_op = -1;
        pr.sem_flg = 0;                                                            
       if (semop(sem, &pr, 1) < 0)
        {
            perror("semop V");
            return -1;
        
        for (int i = 0, j = strlen(temp) - 1; i < j; i++, j--)
        {
            char u = temp[j];
            temp[j] = temp[i];
            temp[i] = u;
        }

        pr.sem_num = 0;
        pr.sem_op = +1;
        pr.sem_flg = 0;
        if (semop(sem, &pr, 1) < 0)
        {
            perror("semop V");
            return -1;
        }
    }

    semctl(sem, 0, IPC_RMID);
    shmctl(mid, IPC_RMID, NULL);

    return 0;
}

1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
^C
ubuntu@ubuntu:~/文件IO/双进程通信$ 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值