2013-03-28 35 views
0

我试图按照教程要求我编辑示例代码,让程序运行两个轮流将歌词输出到歌曲('There's a hole in the bucket')的进程。读取文件并在进程间强制执行操作顺序

我的问题是,该文件被输出作为一个整体,而不是alternativley像它应该见截图什么我谈论:http://imgur.com/NusvhVA

我的代码如下。谢谢。

#include <sys/ipc.h> 
    #include <sys/sem.h> 
    #include <stdio.h> 
    #include <stdlib.h> 

    #define KEY 87654 //Unique semaphore key 

int main() 
{ 
    int id; /* Number by which the semaphore is known within a program */ 
    FILE *file; 
    file = fopen("207song.txt", "r"); 
    int c; 

    union semun { 
    int val; 
    struct semid_ds *buf; 
    ushort * array; 
    } argument; 

    argument.val = 1; 

    /* Create the semaphore with external key KEY if it doesn't already 
    exists. Give permissions to the world. */ 
    id = semget(KEY, 1, 0666 | IPC_CREAT); 

    /* Always check system returns. */ 
    if(id < 0) { 
     fprintf(stderr, "Unable to obtain semaphore.\n"); 
     exit(0); 
    } 

    /* What we actually get is an array of semaphores. The second 
    argument to semget() was the array dimension - in our case 
    1. */ 

    /* Set the value of the number 0 semaphore in semaphore array 
    # id to the value 0. */ 
    if(semctl(id, 0, SETVAL, argument) < 0) { 
     fprintf(stderr, "Cannot set semaphore value.\n"); 
    } else { 
     fprintf(stderr, "Semaphore %d initialized.\n", KEY); 
    } 

    int pid=fork(); 


    const int HENRY_DONE = 0; 
    const int LIZA_DONE = 1; 
    volatile int flag = HENRY_DONE; 

    if(pid) { 
    struct sembuf operations[1]; 
    int retval; /* Return value from semop() */ 

    /* Get the index for the semaphore with external name KEY. */ 
    id = semget(KEY, 1, 0666); 

    if(id < 0){ 
     /* Semaphore does not exist. */ 

     fprintf(stderr, "Program sema cannot find semaphore, exiting.\n"); 
     exit(0); 
    } 
    operations[0].sem_num = 0; 
    /* Which operation? Subtract 1 from semaphore value : */ 
    operations[0].sem_op = -1; 
    /* Set the flag so we will wait : */ 
    operations[0].sem_flg = 0; 

    while(1){ 
     //Process 1 

     //wait 
     operations[0].sem_op = -1; 
     retval = semop(id, operations, 1); 

     //critical section 
     printf("Liza's Part: \n"); 
     fflush(stdout); 
     sleep(1); 

     while ((c = getc(file)) !=EOF) 
        if (c == "\n") { 
          putchar(c); 
          break; 
          } 
        else 
        putchar(c); 
     fflush(stdout); 

     operations[0].sem_op = 1; 
     //signal 
     retval = semop(id, operations, 1); 

    } 
    }else{ 
    //Process 2 
    struct sembuf operations[1]; 
    int retval; /* Return value from semop() */ 
    /* Get the index for the semaphore with external name KEY. */ 
    id = semget(KEY, 1, 0666); 
    if(id < 0){ 
     /* Semaphore does not exist. */ 

     fprintf(stderr, "Program sema cannot find semaphore, exiting.\n"); 
     exit(0); 
    } 
    operations[0].sem_num = 0; 
    /* Which operation? Subtract 1 from semaphore value : */ 

    operations[0].sem_op = -1; 
    /* Set the flag so we will wait : */ 
    operations[0].sem_flg = 0; 

    while(1){ 

     //wait 

     operations[0].sem_op = -1; 
     retval = semop(id, operations, 1); 

     //critical section 

     printf("Henry's Part: \n"); 
     fflush(stdout); 
     sleep(1); 


     while ((c = getc(file)) !=EOF) 
        if (c == "\n") { 
          putchar(c); 
          break; 
          } 
        else 
        putchar(c); 
     fflush(stdout); 

     //signal 
     operations[0].sem_op = 1; 
     retval = semop(id, operations, 1); 

    } 

    } 

} 

回答

0

如果while循环您有:

while ((c = getc(file)) !=EOF) 
       if (c == "\n") { 

GETC返回一个整数, “\ n” 是char类型的C-字符串*。即 比较不匹配,导致第一个消费者显示整个文件。

你可能想

c == '\n' 

注单引号“,而不是双“单引号将是一个字符,这将一个int合理比较。

+0

喜你的答案的工作,但现在我的输出我收到不是我所期望的在这里看到的截图:http://imgur.com/3W1J6NC inteneded出应该是像[亨利:theres一个洞...等,*新线*丽莎:然后修复它。 .etc,* newline * henry:....] – tarantino

+0

这个特殊的构建完整地跳过前两行的henry – tarantino

相关问题