IO进程线程day9

文章提供了一个示例,展示了如何在两个进程中创建共享内存,其中一个进程负责倒置内存中的字符串,而另一个进程则循环打印该字符串。程序避免了使用辅助数组和sleep函数,保证了输出的顺序。通过共享内存中的标志位协调两个进程的执行,确保不会出现乱序。
摘要由CSDN通过智能技术生成

创建两个进程,定义一个共享内存,内存中存储char str[10]= "1234567";要求如下

A循环打印str;

B循环倒置str; 不能使用辅助数组;

要求出现的结果没有乱序,只能出现 1234567 7654321

不允许使用sleep函数

代码逆置

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

int main(int argc,const char * argv[])
{
    int flag = 0;
    key_t key = ftok ("./", 1);
    if (key < 0)
    {
        perror ("ftok");
        return -1;
    }
    int shmid = shmget (key, 128, IPC_CREAT|0664);
    if (shmid < 0)
    {
        perror("shmget");
        return -1;
    }

    void * addr = shmat (shmid, NULL, 0);
    if ((void*)-1 == addr)
    {
        perror("shmat");
        return -1;
    }
    *(int*)addr = 0;
    char buf[8] = "1234567";
    char *ptr = (char*)addr+1;
    strcpy(ptr,buf);
    char c;
    while(1)
    {
        if (*(char*)addr == 0)
        {
            int low = 0;
            int high = strlen(buf)-1;
            
            while(low < high)
            {
                c = *((char*)addr+1+low);
                *((char*)addr+1+low) = *((char*)addr+1+high);
                *((char*)addr+1+high) = c;
                low++;
                high--;
            }
            //char *ptr = (char*)addr+1;
            //strcpy(ptr,buf);
            *(char*)addr = 1;
        }
    }
    return 0;
}

打印

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

int main(int argc,const char * argv[])
{
    key_t key = ftok ("./", 1);
    if (key < 0)
    {
        perror ("ftok");
        return -1;
    }
    int shmid = shmget (key, 128, IPC_CREAT|0664);
    if (shmid < 0)
    {
        perror("shmget");
        return -1;
    }

    void * addr = shmat (shmid, NULL, 0);
    if ((void*)-1 == addr)
    {
        perror("shmat");
        return -1;
    }
    //printf ("%d\n",*(int*)addr);

    while(1)
    {
        if(*(char*)addr == 1)
        {
            printf ("%s\n",(char*)addr+1);
            *(char*)addr = 0;
        }     
    }
    return 0;
}

 

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>
#include <semaphore.h>
struct msgbuf1 {
    long mtype;       
    char mtext[128];   
};
struct msgbuf2 {
    long mtype;       
    char mtext[128];   
};
struct msgbuf1 snd1;
struct msgbuf2 snd2;
sem_t sem1;
sem_t sem2;
void* Tid1 (void* arg)
{
    key_t key = ftok("/home/ubuntu", 1);
    if (key < 0)
    {
        perror("ftok");
        //return NULL;
    }
    int msqid = msgget (key, IPC_CREAT|0664);
    if (msqid < 0)
    {
        perror("msgget");
        //return NULL;
    }
    while(1){
        sem_wait(&sem1);
           // printf ("%d\n", __LINE__);
        printf ("请输入数据类型\n");
        scanf ("%ld", &snd1.mtype);
           // printf ("%d\n", __LINE__);
        getchar();
        //if (0 <= snd1.mtype)
        //{
        //    break;
        //}
        printf ("请输入消息内容\n");
        scanf ("%s", snd1.mtext);
        if (strcasecmp(snd1.mtext, "quit") == 0)
        {
            break;
        }
        getchar();
        if (msgsnd(msqid, &snd1, sizeof(snd1.mtext), 0) < 0)
        {
            perror("msgsnd");
            return NULL;
        }
        //printf ("%d\n", __LINE__);
        sem_post(&sem2);
    }
    msgctl(msqid, IPC_RMID, NULL);
    system ("ipcs -q");
    sem_destroy(&sem1);
    sem_destroy(&sem2); 
    exit(0);
}
void* Tid2 (void* arg)
{
    key_t key = ftok("/home/ubuntu", 1);
    if (key < 0)
    {
        perror("ftok");
        return NULL;
    }
    int msqid = msgget (key, IPC_CREAT|0664);
    if (msqid < 0)
    {
        perror("msgget");
        return NULL;
    }
    while(1)
    {
        sem_wait(&sem2);
        //if( msgrcv(msqid, &snd, sizeof(snd.mtext), 4, 020000|IPC_NOWAIT) < 0)
        if( msgrcv(msqid, &snd2, sizeof(snd2.mtext), 2, 0) < 0)
        {
            perror("msgrcv");
            return NULL;
        }else{
            printf ("%s\n", snd2.mtext); 
             if (strcasecmp(snd2.mtext, "quit") == 0)
                {
                    break;
                }
        }
        sem_post(&sem1);
    }
    msgctl(msqid, IPC_RMID, NULL);
    system ("ipcs -q");
    sem_destroy(&sem1);
    sem_destroy(&sem2); 
    exit(0);
}
int main(int argc,const char * argv[])
{
    
    pthread_t tid1;
    pthread_t tid2;
    if (sem_init (&sem1, 0, 1) < 0)
    {
        perror("seminit");
        return -1;
    }
    if (sem_init (&sem2, 0, 0) < 0)
    {
        perror("seminit");
        return -1;
    }
    if (pthread_create(&tid1, NULL, Tid1, NULL) != 0)
    {
        printf ("lose __%d__", __LINE__);
        return -1;
    }
    //pthread_detach(tid1);
    if (pthread_create(&tid2, NULL, Tid2, NULL) != 0)
    {
        printf ("lose __%d__", __LINE__);
        return -1;
    }
    //pthread_detach(tid2);
    pthread_join (tid2, NULL);
    pthread_join (tid1, NULL);
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <pthread.h>
struct msgbuf1 {
    long mtype;       
    char mtext[128];   
};
struct msgbuf2 {
    long mtype;       
    char mtext[128];   
};
struct msgbuf1 snd1;
struct msgbuf2 snd2;
sem_t sem1;
sem_t sem2;
void* Tid1 (void* arg)
{
    key_t key = ftok("/home/ubuntu", 1);
    if (key < 0)
    {
        perror("ftok");
        return NULL;
    }
    int msqid = msgget (key, IPC_CREAT|0664);
    if (msqid < 0)
    {
        perror("msgget");
        return NULL;
    }
    while(1)
    {
        sem_wait(&sem1);
        //if( msgrcv(msqid, &snd, sizeof(snd.mtext), 4, 020000|IPC_NOWAIT) < 0)
        if( msgrcv(msqid, &snd1, sizeof(snd1.mtext), 1, 0) < 0)
        {
            perror("msgrcv");
        }else{
            printf ("%s\n", snd1.mtext);
            if (strcasecmp(snd1.mtext, "quit") == 0)
            {
                break;
            }
        }
        sem_post(&sem2);
    }
    msgctl(msqid, IPC_RMID, NULL);
    system ("ipcs -q");
    sem_destroy(&sem1);
    sem_destroy(&sem2); 
    exit(0);
}
void* Tid2 (void* arg)
{
    //printf ("%d\n", __LINE__);
    key_t key = ftok("/home/ubuntu", 1);
    if (key < 0)
    {
        perror("ftok");
        return NULL;
    }
    int msqid = msgget (key, IPC_CREAT|0664);
    if (msqid < 0)
    {
        perror("msgget");
        return NULL;
    }
    //printf ("%d\n", __LINE__);
    while(1){
        sem_wait(&sem2);
        printf ("请输入数据类型\n");
        scanf ("%ld", &snd2.mtype);
        getchar();
        //if (0 <= snd2.mtype)
        //{
        //    break;
        //}
        printf ("请输入消息内容\n");
        scanf ("%s", snd2.mtext);
        getchar();
        if (strcasecmp(snd2.mtext, "quit") == 0)
            {
                break;
            }
        if (msgsnd(msqid, &snd2, sizeof(snd2.mtext), 0) < 0)
        {
            perror("msgsnd");
            return NULL;
        }
        sem_post(&sem1);
    }
    msgctl(msqid, IPC_RMID, NULL);
    system ("ipcs -q");
    sem_destroy(&sem1);
    sem_destroy(&sem2); 
    exit(0);
}
int main(int argc,const char * argv[])
{
    pthread_t tid1;
    pthread_t tid2;
    if (pthread_create(&tid1, NULL, Tid1, NULL) != 0)
    {
        printf ("lose __%d__", __LINE__);
        return -1;
    }
    //pthread_detach(tid1);
    if (pthread_create(&tid2, NULL, Tid2, NULL) != 0)
    {
        printf ("lose __%d__", __LINE__);
        return -1;
    }
    //pthread_detach(tid2);
    if (sem_init (&sem1, 0, 1) < 0)
    {
        perror("seminit");
        return -1;
    }
    if (sem_init (&sem2, 0, 0) < 0)
    {
        perror("seminit");
        return -1;
    }
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    return 0;
}

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值