IO进程线程DAY 9

作业一:

msg_1.c:

#include 
#include 
#include 
#include 
#include 

typedef struct msgbuf
{
    long mtype;// >0
    char mtext[128];
}Msgbuf;

void *thread_r(void *);
void *thread_w(void *);

key_t key;
int msqid;
pthread_t tid_r, tid_w;
Msgbuf msgbuf_r, msgbuf_w;

long myType = 1;
long otherType = 2;

int mask = 2;

int main(int argc, char const *argv[])
{
    // 1. ftok
    key = ftok("./", mask);// 8bit
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    else
    {
        printf("ftok : success with key %d.\n", key);
    }
    // 2. msgget
    msqid = msgget(key, IPC_CREAT|0777);
    if(msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    else
    {
        printf("msgget : success with msqid %d.\n", msqid);
    }

    // thread r and w
    if(pthread_create(&tid_r, NULL, thread_r, NULL) < 0)
    {
        perror("pthread_create");
        return -1;
    }
    else
    {
        printf("pthread_create : success with tid_r %ld.\n", tid_r);
    }
    if(pthread_create(&tid_w, NULL, thread_w, NULL) < 0)
    {
        perror("pthread_create");
        return -1;
    }
    else
    {
        printf("pthread_create : success with tid_w %ld.\n", tid_w);
    }

    // block main thread
    pthread_join(tid_r, NULL);
    pthread_join(tid_w, NULL);

    // msgctl
    if(msgctl(msqid, IPC_RMID, NULL) < 0)
    {
        perror("msgctl");
        return -1;
    }
    else
    {
        printf("msgctl : msgqueue exit success.\n");
    }

    return 0;
}

void *thread_r(void *arg)
{
    // msgrcv
    while(1)
    {
        bzero(msgbuf_r.mtext, sizeof(msgbuf_r.mtext));
        msgrcv(msqid, &msgbuf_r, sizeof(msgbuf_r.mtext), otherType, 0);
        if(0 == strcasecmp(msgbuf_r.mtext, "quit"))
        {
            pthread_cancel(tid_w);
            break;
        }
        printf("read from msg_2 >>> %s\n", msgbuf_r.mtext);
    }
    pthread_exit(NULL);
}

void *thread_w(void *arg)
{
    while(1)
    {
        // printf("Please input >>> ");
        msgbuf_w.mtype = myType;
        // scanf("%ld %s", msgbuf_w.mtype, msgbuf_w.mtext);
        // scanf("%s", msgbuf_w.mtext);
        fgets(msgbuf_w.mtext, sizeof(msgbuf_w.mtext), stdin);
        msgbuf_w.mtext[strlen(msgbuf_w.mtext)-1] = 0;
        msgsnd(msqid, &msgbuf_w, sizeof(msgbuf_w.mtext), IPC_NOWAIT);
        if(0 == strcasecmp(msgbuf_w.mtext, "quit"))
        {
            pthread_cancel(tid_r);
            break;
        }
    }
    pthread_exit(NULL);
}

msg_2.c

#include 
#include 
#include 
#include 
#include 

typedef struct msgbuf
{
    long mtype;// >0
    char mtext[128];
}Msgbuf;

void *thread_r(void *);
void *thread_w(void *);

key_t key;
int msqid;
pthread_t tid_r, tid_w;
Msgbuf msgbuf_r, msgbuf_w;

long myType = 2;
long otherType = 1;

int mask = 2;

int main(int argc, char const *argv[])
{
    // 1. ftok
    key = ftok("./", mask);// 8bit
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    else
    {
        printf("ftok : success with key %d.\n", key);
    }
    // 2. msgget
    msqid = msgget(key, IPC_CREAT|0777);
    if(msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    else
    {
        printf("msgget : success with msqid %d.\n", msqid);
    }

    // thread r and w
    if(pthread_create(&tid_r, NULL, thread_r, NULL) < 0)
    {
        perror("pthread_create");
        return -1;
    }
    else
    {
        printf("pthread_create : success with tid_r %ld.\n", tid_r);
    }
    if(pthread_create(&tid_w, NULL, thread_w, NULL) < 0)
    {
        perror("pthread_create");
        return -1;
    }
    else
    {
        printf("pthread_create : success with tid_w %ld.\n", tid_w);
    }

    // block main thread
    pthread_join(tid_r, NULL);
    pthread_join(tid_w, NULL);

    // msgctl
    if(msgctl(msqid, IPC_RMID, NULL) < 0)
    {
        perror("msgctl");
        return -1;
    }
    else
    {
        printf("msgctl : msgqueue exit success.\n");
    }

    return 0;
}

void *thread_r(void *arg)
{
    // msgrcv
    while(1)
    {
        bzero(msgbuf_r.mtext, sizeof(msgbuf_r.mtext));
        msgrcv(msqid, &msgbuf_r, sizeof(msgbuf_r.mtext), otherType, 0);
        if(0 == strcasecmp(msgbuf_r.mtext, "quit"))
        {
            pthread_cancel(tid_w);
            break;
        }
        printf("read from msg_1 >>> %s\n", msgbuf_r.mtext);
    }
    pthread_exit(NULL);
}

void *thread_w(void *arg)
{
    while(1)
    {
        // printf("Please input >>> ");
        msgbuf_w.mtype = myType;
        // scanf("%ld %s", msgbuf_w.mtype, msgbuf_w.mtext);
        // scanf("%s", msgbuf_w.mtext);
        fgets(msgbuf_w.mtext, sizeof(msgbuf_w.mtext), stdin);
        msgbuf_w.mtext[strlen(msgbuf_w.mtext)-1] = 0;
        msgsnd(msqid, &msgbuf_w, sizeof(msgbuf_w.mtext), IPC_NOWAIT);
        if(0 == strcasecmp(msgbuf_w.mtext, "quit"))
        {
            pthread_cancel(tid_r);
            break;
        }
    }
    pthread_exit(NULL);
}

运行截图:

IO进程线程DAY 9_第1张图片

 作业二:

shm_print.c

#include 
#include 
#include 
#include 
#include 

size_t shmSize = 32;

int main(int argc, char const *argv[])
{   
    // 1. ftok
    key_t key = ftok("./", 1);
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    else
    {
        printf("ftok : success with key %d.\n", key);
    }
    // 2. shmid
    int shmid = shmget(key, shmSize, IPC_CREAT | 0777);
    if(shmid < 0)
    {
        perror("shmget");
        return -1;
    }
    else
    {
        printf("shmget : success with shmid %d.\n", shmid);
    }

    // 4. shmat
    char *shmaddr = (char *)shmat(shmid, NULL, 0);
    if((void *)shmaddr < 0)
    {
        perror("shmat");
        return -1;
    }
    else
    {
        printf("shmat : linked with addr %p.\n", shmaddr);
    }

    // write
    bzero(shmaddr, shmSize);
    char *pflag= shmaddr;
    *pflag = 1;
    // 1 >>> can be reversed
    // 0 >>> can be printed
    char *phead = pflag+1;
    strcpy(phead, "1234567");
    
    // print
    while(1)
    {
        if(0 == *pflag)
        {
            printf("%s\n", phead);
            *pflag = 1;
        }
    }

    // following code won't run

    // 5. shmdt
    if(shmdt(shmaddr) < 0)
    {
        perror("shmdt");
        return -1;
    }
    else
    {
        printf("shmdt : link closed.\n");
    }

    // 3. shmctl
    if(shmctl(shmid, IPC_RMID, NULL) < 0)
    {
        perror("shmctl");
        return -1;
    }
    else
    {
        printf("shmctl : shared memory destroyed...\n");
    }

    return 0;
}

shm_reverse.c

#include 
#include 
#include 
#include 
#include 

int main(int argc, char const *argv[])
{   
    // 1. ftok
    key_t key = ftok("./", 1);
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    else
    {
        printf("ftok : success with key %d.\n", key);
    }
    // 2. shmid
    int shmid = shmget(key, 32, IPC_CREAT | 0777);
    if(shmid < 0)
    {
        perror("shmget");
        return -1;
    }
    else
    {
        printf("shmget : success with shmid %d.\n", shmid);
    }

    // 4. shmat
    char *shmaddr = (char *)shmat(shmid, NULL, 0);
    if((void *)shmaddr < 0)
    {
        perror("shmat");
        return -1;
    }
    else
    {
        printf("shmat : linked with addr %p.\n", shmaddr);
    }

    // write
    char *pflag= shmaddr;
    // 1 >>> can be reversed
    // 0 >>> can be printed
    char *phead = pflag+1;
    char *ptail = pflag+strlen(phead);
    char temp = 0;

    // print
    struct shmid_ds buf;
    int count = 0;
    while(1)
    {
        if(1 == *pflag)
        {
            while(phead < ptail)
            {
                temp = *phead;
                *phead = *ptail;
                *ptail = temp;
                phead++;
                ptail--;
            }
            phead = pflag+1;
            ptail = pflag+strlen(phead);
            *pflag = 0;
            count++;// notice count overflow
            // 很多其它方法:如共享内存中写入pid通过kill和signal组合
        }
        shmctl(shmid, IPC_STAT, &buf);
        if(1 == buf.shm_nattch && count)
        {
            printf("while exit.\n");
            break;
        }
    }

    // 5. shmdt
    if(shmdt(shmaddr) < 0)
    {
        perror("shmdt");
        return -1;
    }
    else
    {
        printf("shmdt : link closed.\n");
    }

    // 3. shmctl
    if(shmctl(shmid, IPC_RMID, NULL) < 0)
    {
        perror("shmctl");
        return -1;
    }
    else
    {
        printf("shmctl : shared memory destroyed...\n");
    }

    return 0;
}

运行截图:

IO进程线程DAY 9_第2张图片

 

你可能感兴趣的:(c语言,开发语言)