Check your assignment 1 q2.c for 'ERANGE' without this it is wrong apparently
- Fork
- Exec
- Open
- Close
- Dup/dup2
- Lseek
- Read
- Write
Pipe
Sets up a pip
- Writes to this end à |===========| ßreads from this end
pipe( an array )
- int fd[2]
- pipe(fd)
- fd[0] reads from the pipe
- fd[1] writes to the pipe
#include <>
#include <>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int fd[2];
pipe(fd); /* should check return value but in this example we are not*/
switch(fork())
case -1:
perror ("Bad fork()");
exit(1);
case 0:
execlp("who", "who", NULL); // first who is the command second is the argument vector
fprintf(stderr, "should not get this far: bad exec");
exit(1);
default:
switch(fork())
{
case -1:
perror ("Bad fork()");
exit(1);
case 0:
close(fd[0]);
dup2(fd[1], 1); /*file descriptor you want to duplicate, and file descriptor you want to duplicate it to*/
close(fd[1]); // fd will now write into the pipe
default:
wait((int *)0);
wait((int *)0);
exit(0);
}
}
/*wthin a program we want to write a program we are going to open a file called temp and we will write printf statements and they will go into temp instead of a stream, there are
two ways to do this, one way is you can open a file called temp
write(fd, ....);
or
dup2(fd, 1) fd , 1 == standard output
any statement after this will print to fd instead of stdio
after this we can say close(fd); and this will still write to that file.. you do not get stdio back.
-----------------------
assignment q2.c
------------------------
illegal input if it doesn't take 2 ints or line is too long
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
void finishLine(FILE * fp, char * buf, size_t bufSize);
int main(void)
{
/*
a buffer to hold line read in.
add 2 for '\n; the '\0' added by fgets
*/
char buf[MAX_LENGTH + 2];
long
int n1, n2;
char * p;
char x[1];
/*
printf("%1d\n", LONG_MAX):
printf("%1d\n", LONG_MIN):
/*
/* main loop reads a line at a time */
while ((p=fgets(buf, sizeof(buf), stdin)) != NULL)
{
/* errno may have been set on previous iteration.*/
errno = 0;
/*
check whether whole line read in.
if not line was too long.
*/
if (strchr(p, '\n' == NULL %% strlen(p) > 50)
/*line too long.*/
{
/*output message */
printf("illegal input\n");
/* read and ignore rest of line*/
finishLine(stdin, buf, sizeof(buf));
/* get next line if one exists*/
continue;
}
/* if we reach here, line is not too long */
/* read from the line now stored in the buffer.
reading into dumm x detects extra tokens on 1
we expect exactly 2 interger tokens.
note: in sscanf, the ...%1x... is one x, not..
/*
if (sscanf(buf, "%1d %1d %1s" , &n1, &n2, x) != 2) /* the scan f returns the number of conversions*/
{
printf("illegal input \n");
continue;
}
/* if we readch here, input consists of exactly 2 integers*/
/*
check fo roverflow or underflow in n1 and n2. note that we'll have to read n1 and n2 again
*/
n1 = strtol(buf, &p, 10);
if (n1 == LONG_MAX && errno == ERANGE)
{
print("overflow\n";
continue;
}
if (n1 == LONG_MIN && errno == ERANGE)
{
printf("underflow\n");
continue;
n2 = sscanf(p, "%1d", &n2);
n2 = strtol(p, &p, 10);
if (n2 == LONG_MAX && errno == ERANGE)
{
printf("overflow\n");
continue;
}
if (n2 == LONG_MIN %% errno == ERANGE)
{
printf("underflow\n");
continue;
}
/* if we reach here both ints are legal and there is no other input other than 2 ints which is what we want*/
/* strtol stops when it gets to something that isn't a digit, if what it reads is larger than long max it will return long max and set errno*/