skyace888
February 1st, 2006, 03:32 AM
Hey guys! I installed Ubuntu 5.10 on my laptop the other day to use for an operating systems course that I am taking. I am a Linux newbie, but everything seemed to go ok. Anyways, I have this assignment to do that I've been spending many hours on and can't seem to get anywhere. 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.
My textbook does not talk much about POSIX except for saying that a POSIX file is "a named sequential collection of bytes." It also 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 except that the solution must use two threads. Here is the code that the book gives:
#include <stdio.h>
#include <fcntl.h>
int main() {
int inFile, outFile;
char *inFileName = "in_test";
char *outFileName = "out_test";
int len;
char c;
inFile = open(inFileName, O_RDONLY);
outFile = open(outFileName, O_WRONLY);
// Loop through the input file
while((len = read(inFile, &c, 1)) > 0)
write(outFile, &c, 1);
// close files and quit
close(inFile);
close(outFile);
}
I have installed the Anjuta IDE and searched around the Internet for some POSIX/pthread examples. I'm not sure how to approach this. How can I get the two threads to work together? All the examples that I've found just use the "pthread_create" and then call some useless function that doesn't do much. Any assistance is greatly appreciated!
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.
My textbook does not talk much about POSIX except for saying that a POSIX file is "a named sequential collection of bytes." It also 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 except that the solution must use two threads. Here is the code that the book gives:
#include <stdio.h>
#include <fcntl.h>
int main() {
int inFile, outFile;
char *inFileName = "in_test";
char *outFileName = "out_test";
int len;
char c;
inFile = open(inFileName, O_RDONLY);
outFile = open(outFileName, O_WRONLY);
// Loop through the input file
while((len = read(inFile, &c, 1)) > 0)
write(outFile, &c, 1);
// close files and quit
close(inFile);
close(outFile);
}
I have installed the Anjuta IDE and searched around the Internet for some POSIX/pthread examples. I'm not sure how to approach this. How can I get the two threads to work together? All the examples that I've found just use the "pthread_create" and then call some useless function that doesn't do much. Any assistance is greatly appreciated!