2017-03-23 12 views
-6

私はこのコードを少し編集する必要がありますが、mobaxtermで試してみるとコード自体がコンパイルされないので、C++の経験があまりないので、 。これはコードです:C++コンパイルのエラーメッセージ

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


#include "se207_sems.h" 

int main(int argc, char argv[]){ 
    //Use our source file as the "key" 
    int id=se207_semget("critical_example2.c",1); 

    int pid=fork(); 
    if(pid){ 
    //P1 
    while(1){ 
     se207_wait(id); 
     printf("In critical section P1 ... \n"); 
     rsleep(); 
     printf("Ending critical section P1 ... \n"); 
     se207_signal(id); 
    } 
    }else{ 
    //P2 
    while(1){ 
     se207_wait(id); 
     printf("In critical section P2 ... \n"); 
    rsleep(); 
     printf("Ending critical section P2 ... \n"); 
     se207_signal(id); 
    } 

    } 

} 

は、これは私が取得エラーです:

[email protected]:~$ gcc critical_example2.c 
In file included from critical_example2.c:9:0: 
se207_sems.h: In function ‘se207_wait’: 
se207_sems.h:91:6: warning: type of ‘id’ defaults to ‘int’ [-Wimplicit-int] 
void se207_wait(id){ 
^
se207_sems.h: In function ‘se207_signal’: 
se207_sems.h:95:6: warning: type of ‘id’ defaults to ‘int’ [-Wimplicit-int] 
void se207_signal(id){ 

これは、問題のあるコードのようになります。

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

void rsleep(){ 
    //Random sleep function. Comes in handy demoing stuff. 
    int stime=2+(rand()/(float)(RAND_MAX))*4; 
    printf("Sleeping for %d secs\n",stime); 
    sleep(stime); 
} 


int se207_semget(char* path, int val){ 
    //Very simple semaphore "getting", 
    //always uses 1 as the project ID 
    //takes path to file and initial value of semaphore 

    int id; /* Number by which the semaphore 
     is known within a program */ 


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

    argument.val = val; 

    /* Create the semaphore with external key from 
    ftok if it doesn't already 
    exist. Give permissions to the world. */ 

    id = semget(ftok(path,1), 1, 0666 | IPC_CREAT); 

    /* Always check system returns. */ 

    if(id < 0) 
    { 
     fprintf(stderr, "Unable to obtain semaphore.\n"); 
     exit(0); 
    } 

    /* Set the value of the number 0 semaphore in semaphore array # id 
    to the value "val". */ 

    if(semctl(id, 0, SETVAL, argument) < 0) 
     fprintf(stderr, "Cannot set semaphore value.\n"); 
    else 
    fprintf(stderr, "Semaphore %d initialized with path '%s'.\n", 
      ftok(path,1),path); 
    return id; 
} 

void se207_semop(int id,int val){ 


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

    //simple wait on semaphore 
    operations[0].sem_num = 0; 
    /* Which operation? Subtract 1 from semaphore value to wait, add to 
    signal */ 
    operations[0].sem_op = val; 
    operations[0].sem_flg = 0; 

    retval = semop(id, operations, 1); 

} 


int void se207_wait(id){ 
    se207_semop(id,-1); 
} 

int void se207_signal(id){ 
    se207_semop(id,1); 
} 
+3

'int void se207_wait(id)'これを入力するときに何をしたいですか? – Danh

+2

とにかく、Cとしてコンパイルされましたが、C++でタグ付けされたものよりも頼まれました。 CとC++は2つの異なる言語です – Danh

+0

C++について明示的に尋ねると、私はCタグを削除しました。しかし、あなたはCとしてコンパイルするので、C++タグをCに変更する必要があります。そうでなければ、必要な情報と[mcve]をすべて提供していないとして閉じられます。 – Olaf

答えて

2

あなたse207_waitとse207_signal関数からint型を外し、 idをパラメータリストのintとして宣言します。

void se207_wait(int id){ 
    se207_semop(id,-1); 
} 

void se207_signal(int id){ 
    se207_semop(id,1); 
} 
関連する問題