gpt4 book ai didi

c - C 程序中出现意外标记 "("错误

转载 作者:行者123 更新时间:2023-11-30 17:02:13 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序实现了过山车同步问题( http://www.greenteapress.com/semaphores/downey08semaphores.pdf ),但是当运行下面的代码时,我在第 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;
}

最佳答案

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

gcc -o binary code.c
./binary

关于c - C 程序中出现意外标记 "("错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663823/

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