CIS 505 HW 1 用到的 functions
- pipe() system call returns a pair of file handles and not FILE obj;
- fdopen(3); Use this to convert file handles to FILE obj
FILE *fdopen(int fd, const char *mode);
- pid_t is basically an int
The pid_t data type is a signed integer type which is
capable of representing a process ID. In the GNU C Library, this is an int.
- fdopen() + fgets() + fprintf() 组合建立父子通讯:
- pipe[0] 为读取端口, pipe[1]为写入端口
char rmsg[1000];
FILE *rstream;
rstream = fdopen(dnPipe[i][0], "r");
fgets(rmsg,1000,rstream);
printf("Kid revived %s\n",rmsg);
fclose(rstream);
FILE *wstream;
wstream = fdopen(upPipe[i][1], "w");
fprintf (wstream,"blablabla");
fclose(wstream);
void LoadfromSTDIN(vector<long long> &input){
char rmsg[1000];
FILE *rstream;
rstream = fdopen(STDIN_FILENO, "r");
fgets(rmsg, 1000, rstream);
int n = atoi(rmsg);
cout << n <<endl;
int k = 0;
while(k<n){
k++;
fgets(rmsg, 1000, rstream);
input.push_back(atoll(rmsg));
}
}
- Parse input using getopt()
char *nvalue = NULL;
int nflag = 0, tflag = 0;
int c = 0;
while ((c = getopt(argc, argv, "n:t")) !=-1){
switch (c){
case 'n':
nvalue = optarg;
nflag = 1;
break;
case 't':
tflag = 1;
break;
case '?':
if(optopt == 'n'){
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
} else {
fprintf(stderr, "Unknown option '-%c'.\n", optopt);
}
exit(1);
default:
abort();
}
}
N = atoi(nvalue);
for(index = optind; index < argc; index++){
vector<long long> input;
readFile(input, argv[index]);
Allnums.insert(end(Allnums), begin(input),end(input));
}
- CHILD process don't have to call exec!!!!! for example, in this assignment I have
for(int i = 0; i< N; i++){
pipe(upPipe[i]);
pipe(dnPipe[i]);
pid_t pid = fork();
if(pid < 0){
perror("fork failed\n");
exit(1);
} else if (pid == 0){
vector<long long> input;
close(dnPipe[i][1]);
char rmsg[1000];
FILE * rstream;
rstream = fdopen(dnPipe[i][0], "r");
fgets(rmsg, 1000, rstream);
int n = atoi(rmsg);
for(int j = 0; j < n; j++){
fgets(rmsg, 1000, rstream);
input.push_back(atoll(rmsg));
}
fclose(rstream);
close(dnPipe[i][0]);
bubbleSort(input);
close(upPipe[i][0]);
FILE *wstream;
wstream = fdopen(upPipe[i][1], "w");
fprintf(wstream, "%d\n", (int) input.size());
for(int j = 0; j < (int) input.size(); j++){
fprintf(wstream, "%lld\n", input[j]);
}
fclose(wstream);
close(upPipe[i][1]);
exit(0);
}
}
struct threadPipe_struct{
int upPipe[2];
int dnPipe[2];
};
...
void *threadBubble(void *arg){
vector<long long> input;
struct threadPipe_struct *arg_struct = (struct threadPipe_struct *) arg;
pthread_exit(0);
}
...
struct threadPipe_struct threadPipe[N];
pthread_t tids[N];
for(int i = 0; i < N; i++){
pipe(threadPipe[i].upPipe);
pipe(threadPipe[i].dnPipe);
pthread_attr_t attr;
pthread_attr_init(&attr);
if(pthread_create(&tids[i], &attr, threadBubble, &threadPipe[i]) != 0){
fprintf(stderr, "Unable to create thread!\n");
}
}
...
- A program that checks if seek is possible( if there is stdin!)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(void) {
if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1) {
printf("Cannot do seek.\n"); fflush(stdout);
}
else {
printf("Seek is possible.\n"); fflush(stdout);
}
return 0;
}