作业-11.17

1、两个进程通过消息队列,来半双工通信

        a.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

struct msgbuf{
    long mytype;
    char mtext[128];
};
int msgid;

void* msg_w(void *arg)
{
    struct msgbuf sendmsg;
    sendmsg.mytype = 10;
    while(1)
    {
        memset(sendmsg.mtext, 0, sizeof(sendmsg.mtext));
        scanf("%s", sendmsg.mtext);
        if(msgsnd(msgid, &sendmsg, sizeof(sendmsg.mtext), 0) < 0)
        {
            perror("msgsnd");
            return NULL;
        }
        if(0 == strcmp(sendmsg.mtext, "quit"))
        {
            exit(0);
        }
    }
    pthread_exit(NULL);
}

void* msg_r(void *arg)
{
    struct msgbuf recmsg;
    ssize_t res = 0;
    recmsg.mytype = 100;
    while(1)
    {
    //    memset(recmsg.mtext, 0, sizeof(recmsg.mtext));
        if( res = msgrcv(msgid , &recmsg , sizeof(recmsg.mtext) , 100 , 0) < 0)
        {
            perror("msgrcv");
            return NULL;
        }
        if(0 == strcmp(recmsg.mtext, "quit"))
        {
            exit(0);
        }
        printf("%s\n", recmsg.mtext);
        bzero(recmsg.mtext,sizeof(recmsg.mtext));
    }
    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
    key_t key = ftok("/home/ubuntu/", 2);
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    msgid = msgget(key, IPC_CREAT|0664);
    if(msgid < 0)
    {
        perror("msgget");
        return -1;
    }
    printf("key = %#x, msgid = %d.\n", key, msgid);

    pthread_t pid1, pid2;
    if(pthread_create(&pid1, NULL, msg_w, NULL) != 0)
    {
        perror("pthread_create");
        return -1;
    }
    if(pthread_create(&pid2, NULL, msg_r, NULL) != 0)
    {
        perror("pthread_create");
        return -1;
    }

    pthread_join(pid1, NULL);
    pthread_join(pid2, NULL);

    msgctl(msgid, IPC_RMID, NULL);

    return 0;
}

        b.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

struct msgbuf{
    long mytype;
    char mtext[128];
};
int msgid;

void* msg_w(void *arg)
{
    struct msgbuf sendmsg;
    sendmsg.mytype = 100;
    while(1)
    {
        memset(sendmsg.mtext, 0, sizeof(sendmsg.mtext));
        scanf("%s", sendmsg.mtext);
        if(msgsnd(msgid, &sendmsg, sizeof(sendmsg.mtext), 0) < 0)
        {
            perror("msgsnd");
            return NULL;
        }
        if(0 == strcmp(sendmsg.mtext, "quit"))
        {
            exit(0);
        }
    }
    pthread_exit(NULL);
}

void* msg_r(void *arg)
{
    struct msgbuf recmsg;
    ssize_t res = 0;
    recmsg.mytype = 10;
    while(1)
    {
        //memset(recmsg.mtext, 0, sizeof(recmsg.mtext));
        if(msgrcv(msgid , &recmsg , sizeof(recmsg.mtext) , 10 , 0) < 0)
        {
            perror("msgrcv");
            return NULL;
        }
        if(0 == strcmp(recmsg.mtext, "quit"))
        {
            exit(0);
        }
    
        printf("%s\n", recmsg.mtext);
        bzero(recmsg.mtext,sizeof(recmsg.mtext));
    }
    pthread_exit(NULL);
}

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

    pthread_t pid1, pid2;
    if(pthread_create(&pid1, NULL, msg_w, NULL) != 0)
    {
        perror("pthread_create");
        return -1;
    }
    if(pthread_create(&pid2, NULL, msg_r, NULL) != 0)
    {
        perror("pthread_create");
        return -1;
    }

    pthread_join(pid1, NULL);
    pthread_join(pid2, NULL);

    msgctl(msgid, IPC_RMID, NULL);

    return 0;
}

2、共享内存实现数组打印和倒置

        a.c

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

int main(int argc, const char *argv[])
{
    char str[] = "123456";

    key_t key = ftok("./", 2);
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    int shmid = shmget(key, 32, IPC_CREAT|0777);
    if(shmid < 0)
    {
        perror("shmget");
        return -1;
    }

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

    int flag = 0;
    char *pa = (char*)shmaddr+4;
    *(int*)shmaddr = flag;
    strcpy(pa,str);

    while(1)
    {
        if(0 == flag)
        {
            printf("%s\n", pa);
            flag = 1;
            *(int*)shmaddr = flag;
        }
        flag = *(int*)shmaddr;
        sleep(1);
    }

    shmdt(shmaddr);

    return 0;
}

        b.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

int main(int argc, const char *argv[])
{
    char str[] = "123456";

    key_t key = ftok("./", 2);
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    int shmid = shmget(key, 32, IPC_CREAT|0777);
    if(shmid < 0)
    {
        perror("shmget");
        return -1;
    }

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

    int flag = 0;
    char *pa = (char*)shmaddr+4;
    *(int*)shmaddr = flag;
    int len = strlen(pa);

    printf("len = %d.\n", len);

    while(1)
    {
        if(1 == flag)
        {
            int i=0, j=len-1;
            while(i<j)
            {
                if(*(pa+i) != *(pa+j))
                {
                    *(pa+i) ^= *(pa+j);
                    *(pa+j) ^= *(pa+i);
                    *(pa+i) ^= *(pa+j);
                }
                i++;
                j--;
            }
            flag = 0;
            *(int*)shmaddr = flag;
        }
        flag = *(int*)shmaddr;
    }

    shmdt(shmaddr);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值