gpt4 book ai didi

c - 读取文件并强制进程之间的操作顺序

转载 作者:行者123 更新时间:2023-11-30 17:53:19 24 4
gpt4 key购买 nike

我正在尝试遵循一个教程,该教程要求我编辑示例代码以使程序运行两个进程,这两个进程轮流输出歌曲的歌词(“There's a hole in the bucket”)。

我的问题是文件作为一个整体输出,而不是像我所说的那样应该看到屏幕截图: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);

}

}

}

最佳答案

如果你的 while 循环有:

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

getc 返回一个整数,“\n”是 char* 类型的 C 字符串。那比较不匹配,导致第一个用户显示整个文件。

你可能想要

c == '\n'

注意单引号 '而不是双引号 "单引号将是一个 char,它将与 int 进行合理比较。

关于c - 读取文件并强制进程之间的操作顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15688842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com