Sponsored Content
Top Forums Programming sem_t variable in a C structure???? Post 302595445 by gabam on Friday 3rd of February 2012 06:51:18 AM
Old 02-03-2012
sem_t variable in a C structure????

Hi friends,
I hope you guys are doing well. I am facing this problem, hope you can help me with it. I am trying to create a semaphore inside a structure. The idea is that I am creating a shared memory space between two processes. The reason for a semaphore is that I want some kind of synchronization between the two processes. I think you should see my code, it will become clear to you. I am also including the error that the compiler is giving me.



Code:
 
$ cat producer1.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <sys/sem.h>
#define MEM_SZ 4096
struct shared_use_st
{
        sem_t p;
        sem_t c;
        char some_text[MEM_SZ];
};
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[MEM_SZ];
int shmid;
shmid = shmget((key_t)1234, MEM_SZ, 0666 | IPC_CREAT);
if(shmid == -1)
{
printf("shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, NULL, 0);
if(shared_memory == (void *)-1)
{
printf("\nshmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %x\n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
sem_init(&(shared_stuff->p), 1, 1);
sem_init(&(shared_stuff->c), 1, 0);
while(running)
{
        printf("\nwaiting for client...\n");
        sem_wait(&(shared_stuff->p));
        printf("Enter some text: ");
        fgets(buffer, MEM_SZ, stdin);
        strcpy(shared_stuff->some_text, buffer);
        if(strncmp(buffer, "end", 3) == 0)
        {
                running = 0;
        }
        sem_post(&(shared_stuff->c));
}
if(shmdt(shared_memory) == -1)
{
printf("shmdt failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

And here is what the compiler is telling me.



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

 

10 More Discussions You Might Find Interesting

1. HP-UX

Ram structure

Hi all, I would like know if we can enter a command under UNIX (HPUX 10.xx) to know the hard ram memory structure . Thanks Dorian (1 Reply)
Discussion started by: Dorian
1 Replies

2. UNIX for Dummies Questions & Answers

if then else structure

echo name the file that you want to read read answer if then echo you must enter a file name fi cat $answer im trying to catch the error if user forget to enter the name of the file anyone can help me ? thanks:confused: (4 Replies)
Discussion started by: props
4 Replies

3. Programming

Doubt in structure -- c++

hi, Small sample program #include <iostream> using namespace std; typedef struct vijay { int a; int b; }VIJAY; void main () { VIJAY v; cout << "a=" << v.a<<endl; cout << "b=" << v.b<<endl; if(v) { (5 Replies)
Discussion started by: vijaysabari
5 Replies

4. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

5. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

6. Shell Programming and Scripting

Case structure

Hi, Can anyone help me with the following case structure? echo "Please enter the date for which you want the Report:\c" read dat d1=`echo $dat|cut -c7-8` m1=`echo $dat|cut -c5-6` y1=`echo $dat|cut -c1-4` yr=`expr "$d1" - 1` case "$yr" in 0) MONTH=`expr "$m1" - 1`... (4 Replies)
Discussion started by: kamitsin
4 Replies

7. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

8. UNIX for Dummies Questions & Answers

Size of Structure

How can we find size of a structure with out using sizeof operator? Thanks, Harika (2 Replies)
Discussion started by: harikamamidala
2 Replies

9. UNIX for Dummies Questions & Answers

Directory Structure

Hi... I have a directory which has multiple directories and sub directories inside... what command should i use to get a list of all these directories, without the filenames.... (2 Replies)
Discussion started by: saharookiedba
2 Replies

10. Programming

Structure as global variable

I need to use the below global structure defined in code1.c in another code2.c struct memIOptrs { const char *name; unsigned char *virtptr; }MEM_IO_PTRS; I have a header file for the project codes.h, how should the structure be declared here. Also, what if the structure was... (1 Reply)
Discussion started by: dragonpoint
1 Replies
SEM_INIT(3)						     Linux Programmer's Manual						       SEM_INIT(3)

NAME
sem_init - initialize an unnamed semaphore SYNOPSIS
#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); Link with -pthread. DESCRIPTION
sem_init() initializes the unnamed semaphore at the address pointed to by sem. The value argument specifies the initial value for the sem- aphore. The pshared argument indicates whether this semaphore is to be shared between the threads of a process, or between processes. If pshared has the value 0, then the semaphore is shared between the threads of a process, and should be located at some address that is visible to all threads (e.g., a global variable, or a variable allocated dynamically on the heap). If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)). (Since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore.) Any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), etc. Initializing a semaphore that has already been initialized results in undefined behavior. RETURN VALUE
sem_init() returns 0 on success; on error, -1 is returned, and errno is set to indicate the error. ERRORS
EINVAL value exceeds SEM_VALUE_MAX. ENOSYS pshared is nonzero, but the system does not support process-shared semaphores (see sem_overview(7)). CONFORMING TO
POSIX.1-2001. NOTES
Bizarrely, POSIX.1-2001 does not specify the value that should be returned by a successful call to sem_init(). POSIX.1-2008 rectifies this, specifying the zero return on success. SEE ALSO
sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7) COLOPHON
This page is part of release 3.44 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_INIT(3)
All times are GMT -4. The time now is 06:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy