Sponsored Content
Top Forums Programming Undefined: sem_init, sem_post, sem_wait Post 302582754 by gabam on Sunday 18th of December 2011 09:34:17 AM
Old 12-18-2011
Undefined: sem_init, sem_post, sem_wait

Hi friends,
I am using semaphores in my program, but when I compile the program, it gives the following error


Code:
 
$ gcc sem.c -o sem -lpthread
Undefined                       first referenced
 symbol                             in file
sem_init                            /var/tmp//ccAGJZdT.o
sem_post                            /var/tmp//ccAGJZdT.o
sem_wait                            /var/tmp//ccAGJZdT.o
ld: fatal: Symbol referencing errors. No output written to sem
collect2: ld returned 1 exit status

Or
Code:
$ gcc sem.c -o sem
Undefined first referenced
symbol in file
sem_init /var/tmp//cc3WmF9R.o
sem_post /var/tmp//cc3WmF9R.o
sem_wait /var/tmp//cc3WmF9R.o
ld: fatal: Symbol referencing errors. No output written to sem
collect2: ld returned 1 exit status
$

Here is my program's full source

Code:
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/sem.h>
#define MAX_THREAD 100
typedef struct
{
    int start,end;
} param;
sem_t mysem;
void *count(void *arg)
{
    sem_wait(&mysem);
    int i =0;
    param *p=(param *)arg;
    printf("\nprintfrom %d  to  %d\n",p->start,p->end);
    for(i =p->start ; i< p->end ; i++)
    {
        printf(" i = %d",i);sleep(1);
    }
    sem_post(&mysem);
    return (void*)(1);
}
int main(int argc, char *argv[])
{
    if (sem_init(&mysem,0,1))
    {
    perror("init");
    }
    int n,i;
    pthread_t *threads;
    param *p;
    if (argc != 2)
    {
        printf ("Usage: %s n\n",argv[0]);
        printf ("\twhere n is no. of threads\n");
        exit(1);
    }
    n=atoi(argv[1]);
    if ((n < 1) || (n > MAX_THREAD))
    {
        printf ("arg[1] should be 1 - %d.\n",MAX_THREAD);
        exit(1);
    }
    threads=(pthread_t *)malloc(n*sizeof(*threads));
    p=(param *)malloc(sizeof(param)*n); /* Assign args to a struct and start thread */
    for (i=0; i<n; i++)
    {
        p[i].start=i*100;
        p[i].end=(i+1)*100;
        pthread_create(&threads[i],NULL,count,(void *)(p+i));
    }
    printf("\nWait threads\n");
    sleep(1); /* Wait for all threads. */
    int *x = malloc(sizeof(int));
    for (i=0; i<n; i++)
    {
        pthread_join(threads[i],(void*)x);
    }
        free(p);
        exit(0);
return 0;
}

---------- Post updated at 09:34 AM ---------- Previous update was at 02:28 AM ----------

Nobody seems to bother replying to this query, I guess. Anyways, I found the solution to my problem. This commands works now on my Solaris 10 machine.

Code:
gcc sem.c -o sem -lrt

Thanks anyways, and keep up the good work!
 

9 More Discussions You Might Find Interesting

1. Programming

undefined symbols

i am trying to compile transcode on AIX v 4.3.3 with gcc 3.0.1 i am getting this error: gcc -shared -o .libs/libexport_null.so.0 export_null.o -lpthread -ldl -lc -Wl,-bnoentry -Wl,-bexport:.libs/libexport_null.exp ld: 0711-317 ERROR: Undefined symbol: capability_flag ld: 0711-317... (0 Replies)
Discussion started by: thalex
0 Replies

2. Programming

pthread_mutex_init v. sem_init v. sem_get

Apparently there are three similar features in the linux API. Can someone explain the difference between the data structures that are initialized by these functions: (1) pthread_mutex_init (2) sem_init (3) sem_get Thanks, Siegfried (5 Replies)
Discussion started by: siegfried
5 Replies

3. Shell Programming and Scripting

undefined error

Hi following is a simple shell script, -------------------------------- #!/usr/bin/tcsh u=$USER echo $u string="cp c2960-lanbase-mz /tftpboot/$u/c2960-lanbase-mz" string2="cp c2960-lanlite-mz /tftpboot/$u/c2960-lanlite-mz" output=/users/$u/c2960-log.txt if && then echo... (1 Reply)
Discussion started by: amitrajvarma
1 Replies

4. Homework & Coursework Questions

undefined reference help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i know when undefined reference shows up the program is saying it is not link to that function but the problem... (1 Reply)
Discussion started by: mgyeah
1 Replies

5. Homework & Coursework Questions

undefined reference help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The problem is a function which i typed although it kept saying that it is a undefined reference still. other... (1 Reply)
Discussion started by: mgyeah
1 Replies

6. Programming

sem_wait, sem_post confusion?

Hi friends, I have written this small program using the concept of semaphores. I am a bit confused, hope u can help me with it. The idea is that 1. Two threads are created 2. First will be displaying 0(zero) on the screen 3. Second will be displaing 1(one) on the screen 4. This process... (2 Replies)
Discussion started by: gabam
2 Replies

7. Programming

Undefined Symbol

When I try to link a .so file I get the undefined symbol error. Though I have the library file in my system. The compile and linking commands used are as follows g++ -m64 -g -Wall -I./include -c dxl_sample.c g++ -o firstprogram -m64 -g -Wall -I./include -Bdynamic... (1 Reply)
Discussion started by: Ranadeep Ghosal
1 Replies

8. UNIX for Advanced & Expert Users

Undefined Symbol

When I try to link a .so file I get the undefined symbol error. Though I have the library file in my system. The compile and linking commands used are as follows g++ -m64 -g -Wall -I./include -c dxl_sample.c g++ -o firstprogram -m64 -g -Wall -I./include -Bdynamic... (1 Reply)
Discussion started by: Ranadeep Ghosal
1 Replies

9. Homework & Coursework Questions

Semaphores sem_wait sem_post problem

hallo! if there are many processes running and I initialize the semaphore like this: my_sem = sem_open(SEM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 10); (the last argument is 10) and then i use sem_wait(my_sem); sleep(5); sem_post; Will 10 processes be able to access the... (1 Reply)
Discussion started by: whatevernever
1 Replies
SEMAPHORES(3)						     Library Functions Manual						     SEMAPHORES(3)

NAME
sem_init, sem_wait, sem_trywait, sem_post, sem_getvalue, sem_destroy - operations on semaphores SYNOPSIS
#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_wait(sem_t * sem); int sem_trywait(sem_t * sem); int sem_post(sem_t * sem); int sem_getvalue(sem_t * sem, int * sval); int sem_destroy(sem_t * sem); DESCRIPTION
This manual page documents POSIX 1003.1b semaphores, not to be confused with SystemV semaphores as described in ipc(5), semctl(2) and semop(2). Semaphores are counters for resources shared between threads. The basic operations on semaphores are: increment the counter atomically, and wait until the counter is non-null and decrement it atomically. sem_init initializes the semaphore object pointed to by sem. The count associated with the semaphore is set initially to value. The pshared argument indicates whether the semaphore is local to the current process ( pshared is zero) or is to be shared between several pro- cesses ( pshared is not zero). LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero. sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically decreases the semaphore count. sem_trywait is a non-blocking variant of sem_wait. If the semaphore pointed to by sem has non-zero count, the count is atomically decreased and sem_trywait immediately returns 0. If the semaphore count is zero, sem_trywait immediately returns with error EAGAIN. sem_post atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be used in asynchro- nous signal handlers. sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem. sem_destroy destroys a semaphore object, freeing the resources it might hold. No threads should be waiting on the semaphore at the time sem_destroy is called. In the LinuxThreads implementation, no resources are associated with semaphore objects, thus sem_destroy actually does nothing except checking that no thread is waiting on the semaphore. CANCELLATION
sem_wait is a cancellation point. ASYNC-SIGNAL SAFETY On processors supporting atomic compare-and-swap (Intel 486, Pentium and later, Alpha, PowerPC, MIPS II, Motorola 68k), the sem_post func- tion is async-signal safe and can therefore be called from signal handlers. This is the only thread synchronization function provided by POSIX threads that is async-signal safe. On the Intel 386 and the Sparc, the current LinuxThreads implementation of sem_post is not async-signal safe by lack of the required atomic operations. RETURN VALUE
The sem_wait and sem_getvalue functions always return 0. All other semaphore functions return 0 on success and -1 on error, in addition to writing an error code in errno. ERRORS
The sem_init function sets errno to the following codes on error: EINVAL value exceeds the maximal counter value SEM_VALUE_MAX ENOSYS pshared is not zero The sem_trywait function sets errno to the following error code on error: EAGAIN the semaphore count is currently 0 The sem_post function sets errno to the following error code on error: ERANGE after incrementation, the semaphore value would exceed SEM_VALUE_MAX (the semaphore count is left unchanged in this case) The sem_destroy function sets errno to the following error code on error: EBUSY some threads are currently blocked waiting on the semaphore. AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr> SEE ALSO
pthread_mutex_init(3), pthread_cond_init(3), pthread_cancel(3), ipc(5). LinuxThreads SEMAPHORES(3)
All times are GMT -4. The time now is 06:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy