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
SEM_OVERVIEW(7) 					     Linux Programmer's Manual						   SEM_OVERVIEW(7)

NAME
sem_overview - overview of POSIX semaphores DESCRIPTION
POSIX semaphores allow processes and threads to synchronize their actions. A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one (sem_post(3)); and decrement the semaphore value by one (sem_wait(3)). If the value of a semaphore is currently zero, then a sem_wait(3) operation will block until the value becomes greater than zero. POSIX semaphores come in two forms: named semaphores and unnamed semaphores. Named semaphores A named semaphore is identified by a name of the form /somename; that is, a null-terminated string of up to NAME_MAX-4 (i.e., 251) characters consisting of an initial slash, followed by one or more characters, none of which are slashes. Two processes can operate on the same named semaphore by passing the same name to sem_open(3). The sem_open(3) function creates a new named semaphore or opens an existing named semaphore. After the semaphore has been opened, it can be operated on using sem_post(3) and sem_wait(3). When a process has finished using the semaphore, it can use sem_close(3) to close the semaphore. When all processes have finished using the semaphore, it can be removed from the system using sem_unlink(3). Unnamed semaphores (memory-based semaphores) An unnamed semaphore does not have a name. Instead the semaphore is placed in a region of memory that is shared between multiple threads (a thread-shared semaphore) or processes (a process-shared semaphore). A thread-shared semaphore is placed in an area of memory shared between the threads of a process, for example, a global variable. A process-shared semaphore must be placed in a shared memory region (e.g., a System V shared memory segment created using shmget(2), or a POSIX shared memory object built created using shm_open(3)). Before being used, an unnamed semaphore must be initialized using sem_init(3). It can then be operated on using sem_post(3) and sem_wait(3). When the semaphore is no longer required, and before the memory in which it is located is deallocated, the semaphore should be destroyed using sem_destroy(3). The remainder of this section describes some specific details of the Linux implementation of POSIX semaphores. Versions Prior to kernel 2.6, Linux supported only unnamed, thread-shared semaphores. On a system with Linux 2.6 and a glibc that provides the NPTL threading implementation, a complete implementation of POSIX semaphores is provided. Persistence POSIX named semaphores have kernel persistence: if not removed by sem_unlink(3), a semaphore will exist until the system is shut down. Linking Programs using the POSIX semaphores API must be compiled with cc -pthread to link against the real-time library, librt. Accessing named semaphores via the file system On Linux, named semaphores are created in a virtual file system, normally mounted under /dev/shm, with names of the form sem.somename. (This is the reason that semaphore names are limited to NAME_MAX-4 rather than NAME_MAX characters.) Since Linux 2.6.19, ACLs can be placed on files under this directory, to control object permissions on a per-user and per-group basis. CONFORMING TO
POSIX.1-2001. NOTES
System V semaphores (semget(2), semop(2), etc.) are an older semaphore API. POSIX semaphores provide a simpler, and better designed inter- face than System V semaphores; on the other hand POSIX semaphores are less widely available (especially on older systems) than System V semaphores. EXAMPLE
An example of the use of various POSIX semaphore functions is shown in sem_wait(3). SEE ALSO
sem_close(3), sem_destroy(3), sem_getvalue(3), sem_init(3), sem_open(3), sem_post(3), sem_unlink(3), sem_wait(3), pthreads(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2012-05-13 SEM_OVERVIEW(7)
All times are GMT -4. The time now is 03:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy