Tuesday, March 11, 2008

CSE 2031 Lecture 15

Man ulimit | col 0b > foo.mantxt


 

Filesystem

  • Filesystem is an organized structure consisting of many files and / or directories
  • A file is a stream of bytes
    • Stores data
    • Has a name
    • Usually on disk or some other storage medium
  • A directory is a special file in the filesystem which contains files and other directories
  • Unix filesystem
    • Bin
    • Usr
    • Etc
    • Users
      • Admin
        • John
        • steve
      • Staff
      • student
    • lib
  • drwx------
    • read write execute
    • d = filetype
      • d for directory
    • a normal file would have a – where the d is
  • %ln marks cpOfMarks
    • Creates a new link to marks
    • Puts in a new name associated with the same inode number
  • Inode
  • Name
  • 1042
  • Temp
  • 3784
  • Prog.c
  • 4798
  • Marks
  • 4798
  • cpOfMarks
  • you need links if you have a file on another partition
  • pwd shows the current working directory
  • cd changes current directory
  • a path name is a reference to something in the filesystem
  • a path name specifies the set of directories you have to pass through to find a file
  • directory names are separated by "/" in Unix
  • special elements : "." And ".."
  • "." Means "the current directory"
  • ".." means "the parent directory"
  • /dec/tty – the terminal you are using
  • /dev / zero – an input stream which returns an edless stream of null bytes
  • ls day?
    • Ls –ld day?
      • Ls –ld day1 day2 day3 day4 day5
    • The ? goes thru all the files that say day
    • And then wildcards the rest of the names

Thursday, March 6, 2008

2031 Lecture 14

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*/


 

Tuesday, March 4, 2008

CSE 2031 Lecture 13

fork()

getpid() // get process ID

getppid() // get parent process ID


 

fork is a system call w/ no arguments but it returns a value

the parent and the child see different return values

the newly created child see's return value 0

the parent that called fork see's a nonzero return value, namely the ppid of the newly created child


 

a child finds a parents process ID by calling getppid()


 

when a child terminates, OS saves PID, exit status, cpu time ... of child

and passes this to the parent when parent calls wait() or waitpid


 

until then the child is a 'zombie'

the OS can't free up the resources for the child until the parent waits on the child

if the parent finishes before the child, then the child is reparented

    wait

            - block until first child finishes

    waitpid

        - block

        -specify which child to wait for

pid of child <- wait(int*)

int status

wait(&status)


 

%a.out

    - what we have here is the shell foks off a child and waits for the child to return

    child - executes a.out

    once done the parent outputs the prompt and waits for another command to be inputted

if %a.out &

    -the shell doesn't wait.

-----------------------------------------------------------------------------------------------------------------------------------------------


 

#include <stdio.h>

#include <sys/wait.h>

#include <sys/types.h>

#include <unistd.h>

#include <stlib.h>


 

#define MAX 10

int main(void)

{


 

int i;

fork();

fork();

printf("Hello\n");

return 0;

}


 

// prints 4 'Hello" lines

-----------------------------------------------------------------------------------------------------------------------------------------------


 

#include <stdio.h>

#include <sys/wait.h>

#include <sys/types.h>

#include <unistd.h>

#include <stlib.h>


 

#define MAX 10

int main(void)

{


 

int i;


 

switch(fork())

{

case -1:

    perror("Bad fork");

    exit(1)

case 0:

    /* CHILD */

    for (i=0; i < MAX; i++)

    {

        printf("CHILD\n");

    }break;

default:

    /* PARENT */

    for(i = 0; i < MAX; i++)

    {

        printf("\tPARENT\n");

    }

    printf("pid = %d\n", getpid());

    printf("ppid = %d\n", getppid());

return 0;    


 

}


 

/*prints CHILD 10 times, pid = 2153, ppid = 2152, prints PARENT 10 times then , pid = 2255, ppid = 27289

eventually the parent executes before the child because it is left to the OS to decide the PPID*/

/* if wait((int * ) 0);

was typed after /*PARENT*/ it will always print CHILD first*/


 

along with fork() there is exec()


 

%a.out hello there world becomes


 

argv -> -> a.out

->hello

->there

->world

->null


 

-------------------------------------------------

execlp("cat", "-n", "temp", NULL);

"cat"