意外的令牌"(" C程序中的错误

时间:2016-04-16 11:52:18

标签: c process synchronization

我正在尝试编写一个程序,它实现了过山车同步问题(http://www.greenteapress.com/semaphores/downey08semaphores.pdf),但是当运行下面的代码时,我会在意外令牌附近发现#34;语法错误' ('"在第12行,其中void free_resources(void)是。我已经尝试使用valgrinddos2unix来运行此操作,并且没有找到任何内容错误,所以我认为实现本身是错误的。任何想法为什么?提前感谢。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <time.h>
#include <signal.h>

void free_resources(void);

sem_t *load, *load_end, *unload, *unload_end;
int *I = NULL, *A = NULL, counterId = 0, *Ip = NULL, pcounterId = 0;

void free_resources(void)
{
    free(I);
    shmctl(counterId, IPC_RMID, NULL);
    shmctl(pcounterId, IPC_RMID, NULL);
    sem_close(load);
    sem_close(load_end);
    sem_close(unload);
    sem_close(unload_end);

    sem_unlink("/xnesmel");
    sem_unlink("/xnesmeln");
    sem_unlink("/xnesmeu");
    sem_unlink("/xnesmeun");
}

void resources(){
if( (counterId = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666)) == -1 ){
// handle error
}

if( (pcounterId = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666)) == -1 ){
// handle error
}

if( (I = (int *) shmat(counterId, NULL, 0)) == NULL ){
// handle error
}

if( (A = (int *) malloc(sizeof(int))) == NULL ){
// handle error
}

if( (Ip = (int *) shmat(pcounterId, NULL, 0)) == NULL ){
// handle error
}

*I = 0;
*Ip = 0;
*A = 0;

if( (load = sem_open("/xnesmel", O_CREAT | O_EXCL, 0666, 0)) == SEM_FAILED ){
// handle error
}

if( (load_end = sem_open("/xnesmeln", O_CREAT | O_EXCL, 0666, 0)) == SEM_FAILED ){
// handle error
}

if( (unload = sem_open("/xnesmeu", O_CREAT | O_EXCL, 0666, 0)) == SEM_FAILED ){
// handle error
}

if( (unload_end = sem_open("/xnesmeun", O_CREAT | O_EXCL, 0666, 0)) == SEM_FAILED ){
// handle error
}
}


int main(int argc, char **argv)
{
if(argc != 2){
//error
}
setbuf(stdout, NULL);
resources();



int C = atoi(argv[2]);
int P = atoi(argv[1]);
int N = P/C;

pid_t car = fork();

if (car == 0){
printf("%d: C %d: started\n", (*A)++, (*I)++);
for(int i=0; i != N; i++){
printf("%d: C %d: load\n", (*A)++, i+1);
/*for(int j=0; j != C; j++){
sem_post(load);
}*/
sem_init(load, 0, C);
sem_wait(load_end);
printf("%d: C %d: run\n", (*A)++, i+1);
//uspi
printf("%d: C %d: unload\n", (*A)++, i+1);
sem_init(unload, 0, C);
sem_wait(unload_end);
}
printf("%d: C %d: finished\n", (*A)++, N);
exit(0);
}

for(int i=0; i != P; i++){
pid_t pas = fork();

if (pas == 0){
printf("%d: P %d: started\n", (*A)++, (*Ip)++);

int n, *nn = &n;
sem_wait(load);
sem_getvalue(load, nn);
printf("%d: P %d: board\n", (*A)++, i+1);
if((C-n)>0){
printf("%d: P %d: board order %d\n", (*A)++, i+1, C-n);
} else {
printf("%d: P %d: board last\n", (*A)++, i+1);
sem_post(load_end);
}

int k, *kek = &k;
sem_wait(unload);
sem_getvalue(unload, kek);
printf("%d: P %d: unboard\n", (*A)++, i+1);
if((C-k)>0){
printf("%d: P %d: unboard order %d\n", (*A)++, i+1, C-k);
} else {
printf("%d: P %d: unboard last\n", (*A)++, i+1);
sem_post(unload_end);
}

printf("%d: P %d: finished\n", (*A)++, i+1);
exit(0);
}
}

for (int i = 0; i < P; ++i)
{
    pid_t pid = waitpid(-1, NULL, 0);
    if (pid < 0)
    {
            perror ("waitpid");
            break;
    }
}

free_resources();

return 0;
}

1 个答案:

答案 0 :(得分:0)

您似乎尝试直接在shell中运行C源代码。但这显然不会奏效。您需要首先编译源代码以获取可执行文件,然后可以在shell中运行。例如:

gcc -o binary code.c
./binary