Note that there are some explanatory texts on larger screens.

plurals
  1. POPosix Semaphore compilation error using the -lrt
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/4916881/sem-open-error-undefined-reference-to-sem-open-on-linux-ubuntu-10-10">sem_open() error: &ldquo;undefined reference to sem_open()&rdquo; on linux (Ubuntu 10.10)</a> </p> </blockquote> <p>Having issues with compilation of posix semaphores. My goal is to create a shared memory segment and protect it by semaphores. shared memory works fine but semaphores code gives me compilation errors even though I included semaphore.h and added -lrt to compilation flags</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;errno.h&gt; #include &lt;sys/ipc.h&gt; #include &lt;sys/shm.h&gt; #include &lt;semaphore.h&gt; #include &lt;time.h&gt; int main (int argc, char** argv) { FILE *configFile; int i,j; int CashDesksNo=0; int maxCashDesksNo; int n,m; int TmaxServe; int custPerc; //customer ratio policy int maxCapacity; char * nlptr=NULL; char * pch=NULL; char line[125]; char termInput[30]; char tmpString[40]; int flg1,flg2; int flag; int execResult=0; int fd; int rc; int randNum; int status; pid_t ch_pid; int a,b,c; int shmid=0; int *shm_ptr; int * err; int retval; sem_t *sp; char semName[10]; strcpy(semName,"mutex"); //---------- Davasma kai elegxos in line parameters----------------- // read inline params and config file . . . //------------ Print Configuration Data ---------------------------- if(CashDesksNo&gt;maxCashDesksNo || CashDesksNo&lt;1) { printf("\n# of Cash Desks should be between 1 and %d",maxCashDesksNo); printf("\nsupermarket will now exit...\n"); exit(1); } printf("\n//-----------------------------------------------"); printf("\nSupermarket initialization"); printf("\n# of Cash Desks: %d",CashDesksNo); printf("\n# of max products: %d",n); printf("\nMax price: %d",m); printf("\nMaximum serving time(secs): %d",TmaxServe); printf("\n%% Customer/Cashier Percentage: %d/%d",custPerc,100-custPerc); printf("\nMax Supermarket capacity: %d",maxCapacity); printf("\n//-----------------------------------------------\n"); printf("\nAbout to create customer and cashier processes"); // ----------- Shared Memory Attachment -------------------------------- shmid=shmget(IPC_PRIVATE,sizeof(int*),0666); if (shmid &lt; 0) { perror("shmget"); exit(1); } shm_ptr=(int*)shmat(shmid,(void *)0,0); if (shm_ptr == (int *)(-1)) { perror("shmat"); exit(1); } a=0; //shm shm_ptr=(int*)a; printf("shmPtr:%d",(int)shm_ptr); // ----- create &amp; initialize semaphore --------------------------------- sp = sem_open(semName,O_CREAT,0644,1); if(sp == SEM_FAILED) { perror("unable to create semaphore"); exit(-1); } while(1) { printf("\nPress enter to start a new day at the supermarket(type exit to quit)\n"); fgets(termInput,sizeof(termInput),stdin); nlptr = strchr(termInput, '\n');// termatismos string if (nlptr) *nlptr = '\0'; if(strcmp(termInput,"exit")==0)//exit apo tin efarmogi { printf("\nExiting Supermarket..\n"); exit(0); } i=0; while(i&lt;maxCapacity) { //-Fork new process for the simulation -------------------- ch_pid = fork(); if (ch_pid == -1) { perror("\nFailed to fork initial spliter/merger process \n"); exit(1); } if ( ch_pid == 0 ) //this is the child process { srand (getpid());//pid based seed // "itoa" - Metatropi ari8mou se string sprintf( tmpString, "%d", shmid ); randNum=rand() % 100 + 1; if(randNum&lt;custPerc)// customer : cashier ratio { execResult=execl("customer","customer","0",tmpString,NULL); //printf("\nCreated a customer,randNum %d",randNum); }else { execResult=execl("cashier","cashier","0",tmpString,NULL); //printf("\nCreated a cashier,randNum %d",randNum); } if(execResult==-1) { perror("Could not perform exec to create cashier/customer process"); } } i++; } //Root process //wait for childs to terminate for (i = 0; i &lt; maxCapacity; ++i) { waitpid(-1,&amp;status,0); } printf("supermarket shared memory content: %d",(int)shm_ptr); sem_close(sp); sem_unlink(semName); err = (int*)shmctl(shmid, IPC_RMID, 0); if (err == -1) perror ("Shared Memory Removal."); else printf("Shared Memory Removed. %d\n", (int)(err)); } } </code></pre> <p>this is the makefile:</p> <pre><code>OBJS = supermarket.o cashier.o customer.o SOURCE = supermarket.c cashier.c customer.c HEADER = struct.h OUT = supermarket cashier customer CC = gcc FLAGS = -lrt -g -c LIBS = -lm # -g option enables debugging mode # -c flag generates object code for separate files # -lm math library all: supermarket cashier customer supermarket: supermarket.c $(CC) supermarket.c -o supermarket cashier: cashier.c $(CC) cashier.c -o cashier customer: customer.c $(CC) customer.c -o customer # clean house clean: rm -f $(OBJS) $(OUT) # do a bit of accounting count: wc $(SOURCE) $(HEADER) </code></pre> <p>I keep getting this error:</p> <pre><code>george@george-System-Product-Name:~/Desktop/prj3$ make gcc supermarket.c -o supermarket supermarket.c: In function ‘main’: supermarket.c:310:11: warning: comparison between pointer and integer [enabled by default] /tmp/ccYxA2Wi.o: In function `main': supermarket.c:(.text+0x75c): undefined reference to `sem_open' supermarket.c:(.text+0x9b0): undefined reference to `sem_close' supermarket.c:(.text+0x9bf): undefined reference to `sem_unlink' collect2: ld returned 1 exit status make: *** [supermarket] Error 1 </code></pre> <p>what can i do? this is a project for operating systems class, my system is linux Ubuntu</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload