POSIX Question

aceman817

Senior member
Jul 15, 2001
204
0
0
So I'm taking this operating system course and I came across a question that is a little confusing. Let me also say that I am not looking for someone to simply provide me with an answer, instead some assistance or good web site links would be greatly appreciated. The question is as follows:

POSIX defines a standard thread package in the context of the C programming language. Several manufacturers provide a POSIX thread package as a user library along with their C programming facilities. If you have a system available to you that supports threads, then design and implement a thread program so that one thread reads a file, while a second thread writes the data to another file.

I assume that my system supports threads. I only have one main machine running XP. My textbook gives an example Linux program that opens two files, and then copies the contents of one file into the other one byte at a time using the POSIX interface. I assume that this is similar to the answer exept that the solution must use two threads.

Thanks in advance!
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
You can use pthreads on Linux as the thread library. One solution to this problem is to maintain a queue where the reader thread reads the input file and adds the data to the queue while the writer thread reads the queue and writes the data to the output file. However, you must synchronize access to the queue since you will have multiple threads accessing it. I assume you have been made very familiar with Critical Section objects in you class?
 

aceman817

Senior member
Jul 15, 2001
204
0
0
Thanks for the quick reply. We have not done anything with critical section objects yet though I have a rough understanding that they are sections which may not be accessesd while another section of code is running. They don't mention these in the textbook until several chapters later. If I am going to use a Linux thread library, shouldn't I have a Linux system to work with? I presently do not. Is there a way I can test it from a "live" Linux CD such as Ubuntu so I won't have to repartition my main machine? Or is there a way I can devise a solution in Microsoft Visual Studio for Windows?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: aceman817
Thanks for the quick reply. We have not done anything with critical section objects yet though I have a rough understanding that they are sections which may not be accessesd while another section of code is running. They don't mention these in the textbook until several chapters later. If I am going to use a Linux thread library, shouldn't I have a Linux system to work with? I presently do not. Is there a way I can test it from a "live" Linux CD such as Ubuntu so I won't have to repartition my main machine? Or is there a way I can devise a solution in Microsoft Visual Studio for Windows?

Yes, you will need some sort of a *nix OS. I think a live CD should work if it has gcc/pthreads installed. Perhaps someone else can chime in. Can't you ssh in from your home machine to your University's *nix machines? I am also surprised that you haven't been taught about synchronization and yet you have been given a program that uses multiple threads.
 

aceman817

Senior member
Jul 15, 2001
204
0
0

singh

Golden Member
Jul 5, 2001
1,449
0
0
Then you need to go back to your instructor and ask him how you're supposed to complete the program with such limited information and a lack of knowledge about synchronization.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
I wrote my instructor about this question a few days ago and her response was just to "explore the POSIX threads." At this point, I simply need to do whatever it takes and learn the required material so I may complete the assignment correctly and on time. I need to find out exactly what POSIX is and if it can be implemented in C using the Visual Studio environment in Windows. If not, I will have to find out which tool is available on the Ubuntu Live CD to help me write C code and compile and test it. I am still somewhat of a newbie when it comes to Linux. I also need to document the source that I referenced when I work on the assignment.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
Well I just finished most of the day repartitioning and installing Ubuntu Linux 5.10 on my laptop. It seems to be up and running fine (as I write to you from it within Firefox). I have downloaded the Anjuta IDE and am playing around with it to test my source code. Regarding the original POSIX problem that I need to complete, how can I modify the source code that I provided to satisfy the requirement of the problem? Is there a reference that I can use?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
This won't help you too much, but one clarification: POSIX is an api standard that covers a lot more than threading. The threading part, like the others, is just a spec that says "If you make these calls, your program will behave in this certain way" so that you can write your code once and run it on any POSIX compliant system.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
If that Ubuntu install comes with all of the development tools and man pages, and it should AFAIK, you can do 'man pthreads' to get a brief introduction to pthreads and at the bottom of that man page is a SEE ALSO section with some of the function specific pthread man pages. Also you should be able to do 'man -k pthread' to get a list of all of the man pages related to pthread on your system, 'man -k <term>' is a basic search function.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
When I type "man pthreads" at the terminal, it says there is no manual entry. Do I need to install something?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
It's in the 'manpages' package here on Debian sid and I can't find anything relevant on packages.ubuntu.com right now so I'm installing Breezy in qemu. Once that's finished I'll poke around and see if I can't find out where the pthreads stuff is, but sadly qemu isn't very fast and I gotta get to bed so it's gonna be a while.
 

manly

Lifer
Jan 25, 2000
12,623
3,411
136
pthreads is just a library. Read Stevens' Networking book if you want to figure this stuff out.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
Thanks again for all your help guys. If you find anything out, please let me know. I will continue to do as much research as I possibly can on the subject as the assignment is due tomorrow.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
I took a look at the example over at http://en.wikibooks.org/wiki/ComputerScience:Distributed_Systems under the "Simple pthread example." I wanted to just run that in the Anjuta IDE that I have in Ubuntu Linux and I am receiving numerous errors when I try and build. It gives me several undefined reference errors to 'pthread_create' and 'pthread_join'. If this program won't run, then there must be something wrong/missing on my system. Help me!! I have spent many hours on this problem and need to finish it by tomorrow evening!
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Compiling that example with 'gcc -lpthread -o pthreadfun pthreadfun.c' works fine here. Do you have libc6-dev installed? Also, if you don't have glibc-doc installed, install that too.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
I don't know what I have installed. I'm a newbie when it comes to Linux. I was hoping that this course would show me a little bit about Linux before we started doing work like this! How can I check to see if I have those things installed? All I know is that I am running Ubuntu 5.10 and I installed the Anjuta IDE from the "Add Applications" section of the OS.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
I think I stumbled upon a section that helped me figure out how to successfully compile and build the app. I included the pthread library and the sample file now executes fine. So I should be able to proceed with my problem now, somehow...
 

aceman817

Senior member
Jul 15, 2001
204
0
0
The part I don't understand is how to set it up. In the example I provided, it seems like one character is being read at a time from the file and then it is written to the other file by reference. All the Posix/Pthread examples that I have come across show a separate function that handles the threads. If I am to create two new threads, how can I "syncronize" them to do their tasks of reading and writing together? Pardon my ignorance, but we are only in the second chapter of this online operating systems course and there was no mention of how much C programming would be involved. We never even formally learned C! The first programming course was a basic primer in C++ that I took almost 2 years ago and the second and third were all in Java. I've completed the other problems ok but this one is really confusing me and the structure of the course is not helping things.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
What do you mean by "lock"? I have looked over that page before and it shows simple examples POSIX programs but I don't see I reference to lock.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
Following the examples I've found online, I have come up with the following code:

#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>


void *parseFile(void *); // function prototype


int main(void)
{
int ret1;
int t = 0;

pthread_t firstThread;

ret1 = pthread_create (&firstThread, NULL, parseFile, (void *)t);

pthread_exit(NULL);

return 0;
}

void *parseFile(void *data) {
char *inFileName = "in_test";
char *outFileName = "out_test";
int inFile;
int outFile;
int len;
char c;

outFile = open(outFileName, O_WRONLY);
inFile = open(inFileName, O_RDONLY);
while((len = read(inFile, &c, 1)) > 0)
write(outFile, &c, 1);
close(inFile);
pthread_exit(NULL);
}

The program works correctly but I don't think that this is they way the intructions say to do it. I think I am only creating one thread here instead of two.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
What do you mean by "lock"? I have looked over that page before and it shows simple examples POSIX programs but I don't see I reference to lock.

The page talks about locking/unlocking mutexes, which are a kind of lock.

The program works correctly but I don't think that this is they way the intructions say to do it. I think I am only creating one thread here instead of two.

Please use the "Add Code" option to add code to a post so the formatting stays intact, but yea I think you're right in that it's not exactly what your prof wants since you only have 1 thread.