I am trying to program with semaphores and was told that I should use sem.h. When I compile, I get the error: "sys/sem.h: No such file or directory". How can I fix my problem?

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


int main() {
    printf("I am alive \n");
    getchar();
    return 0;
}

Recommended Answers

All 14 Replies

Which operating system are we talking about here?

Which operating system are we talking about here?

Sorry. I am on Windows 7 Pro

Sorry. I am on Windows 7 Pro

I know that file exists in Linux but I'm not so sure about Windows...

I did a search on my hard drive and could not find the file sem.h, so I downloaded one. Where would I place it?

I did a search on my hard drive and could not find the file sem.h, so I downloaded one. Where would I place it?

Downloading a header file will do nothing, you will need the compiled library or object file so that the main executable can link to it...Are you sure that this is a valid Windows file?

check this windows link that uses semaphores:

http://msdn.microsoft.com/en-us/library/ms686946(VS.85).aspx

Sorry, can you elaborate on your explanation? I don't quite understand it.

I am using Code::Blocks, and I think the compiler is called Ming. I went to C:\Program Files (x86)\CodeBlocks\MinGW\include and I placed my sem.h. When I tried to compile my earlier code, I got lots of errors referencing sem.h. I don't think it is a proper file. I downloaded the file from my school's remote computer. I have tried running my above code on my school's computer, but it produced the same error.

I have placed a copy of sem.h on my web space if you wanted to look at it:
http://www.sfu.ca/~jca41/stuph/sem.h

Sorry, I am very new to programming in C.

In order for the functions in the header file to work, you need the code that is actually behind the function. That code is found in a library file.

Look at it this way:
The header file is like the phone book. It gives you the people and address of their house. But it's only an indication of where to find the house. The library file defines the house itself. How big it is, how many people live there, number of trees in the yard, ect. Without the existence of 'the house' the phone book isn't really useful.

What I can make out from your postings is - your using Windows to run Cygwin. Is that correct? If you are then try reinstalling Cygwin because you seem to be missing some of your libraries.....

Oh, I think I understand how the functions in the header files work. Now is there a way for me to 'install' the missing code in the library file?

I'm not using Windows to run Cygwin. I just Googled Cygwin, and from my understanding Cygwin allows you to run Linux in a Windows environment. I am not trying to simulate a Linux environment, but should I be if I am playing around with semaphores?

Please explain why you need sem.h. Who told you?

Where did you get the code? What compiler did they use? What OS?

Oh, I think I understand how the functions in the header files work. Now is there a way for me to 'install' the missing code in the library file?

I'm not using Windows to run Cygwin. I just Googled Cygwin, and from my understanding Cygwin allows you to run Linux in a Windows environment. I am not trying to simulate a Linux environment, but should I be if I am playing around with semaphores?

You do not need that. Check out the MSDN link posted by Gerad in one of the earlier posts. Try running that code.

I am working on one of my assignments for my Operating Systems class in post secondary. This class is one of my electives, and I don't have much experience programming in C.

The instructions on my assignment involves programming using semaphores, and my prof said that we must use POSIX semaphore library sem.h.

I got the code off a computer on campus. I did a search on its hard drive for the file 'sem.h'. How do we find out which compiler they are running? On campus, I always use Microsoft Visual C++. They are running Windows Server 2003 Enterprise SP2.

Then you need to ask the instructor how to link and compile with POSIX sem.h. It seems to me that's his job to help you set up non-standard functionality.

Yes, I have asked the instructor on Friday and am awaiting her reply. I figured I'd get a head start by asking around on the forums.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.