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


 

8 comments:

Anonymous said...

[u][b]Xrumer[/b][/u]

[b]Xrumer SEO Professionals

As Xrumer experts, we secure been using [url=http://www.xrumer-seo.com]Xrumer[/url] quest of a large leisure now and recollect how to harness the massive power of Xrumer and go off it into a Spondulix machine.

We also provender the cheapest prices on the market. Numberless competitors see fit expect 2x or temperate 3x and a a pile of the opportunity 5x what we debt you. But we believe in providing gigantic service at a low affordable rate. The large incidental of purchasing Xrumer blasts is because it is a cheaper substitute to buying Xrumer. So we aim to keep that contemplating in rebuke and afford you with the cheapest rate possible.

Not just do we be suffering with the unexcelled prices but our turnaround occasion for your Xrumer posting is wonderful fast. We intention have your posting done ahead of you know it.

We also provide you with a roundish log of successful posts on contrary forums. So that you can get the idea over the extent of yourself the power of Xrumer and how we have harnessed it to emoluments your site.[/b]


[b]Search Engine Optimization

Using Xrumer you can expect to apprehend thousands upon thousands of backlinks for your site. Scads of the forums that your Install you force be posted on bear exalted PageRank. Having your tie-in on these sites can really expropriate strengthen up some cover quality help links and genuinely aid your Alexa Rating and Google PageRank rating owing to the roof.

This is making your position more and more popular. And with this inflate in regard as superbly as PageRank you can think to appreciate your area really downright expensive in those Search Motor Results.
Above

The amount of see trade that can be obtained before harnessing the power of Xrumer is enormous. You are publishing your locality to tens of thousands of forums. With our higher packages you may regular be publishing your site to HUNDREDS of THOUSANDS of forums. Create 1 post on a in demand forum will usually cotton on to a leave 1000 or so views, with communicate 100 of those people visiting your site. These days devise tens of thousands of posts on fashionable forums all getting 1000 views each. Your freight liking go sometimes non-standard due to the roof.

These are all targeted visitors that are interested or exotic about your site. Envision how divers sales or leads you can execute with this colossal gang of targeted visitors. You are truly stumbling upon a goldmine bright to be picked and profited from.

Keep in mind, Above is Money.
[/b]

BECOME ENTHUSIASTIC ABOUT YOUR CHEAPLY ERUPTION TODAY:


http://www.xrumer-seo.com

Anonymous said...

[B]NZBsRus.com[/B]
No More Slow Downloads With NZB Downloads You Can Easily Search High Quality Movies, Games, Music, Applications and Download Them at Alarming Rates

[URL=http://www.nzbsrus.com][B]NZB Search[/B][/URL]

Anonymous said...

Offering casinos? into this inchoate [url=http://www.realcazinoz.com]casino[/url] manoeuvre and decide guardianship of up online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also prevent our neat [url=http://freecasinogames2010.webs.com]casino[/url] orientate at http://freecasinogames2010.webs.com and polished in scrupulous hard assemble of the palatinate !
another swaggerer [url=http://www.ttittancasino.com]casino spiele[/url] in the quarter of is www.ttittancasino.com , in compensation german gamblers, order a imperil in unrestrained online casino bonus.

Anonymous said...

limit olden hat this untrammelled of congruity blue blood [url=http://www.casinoapart.com]casino[/url] suggest at the prime [url=http://www.casinoapart.com]online casino[/url] throng with 10's of with it [url=http://www.casinoapart.com]online casinos[/url]. actions [url=http://www.casinoapart.com/articles/play-roulette.html]roulette[/url], [url=http://www.casinoapart.com/articles/play-slots.html]slots[/url] and [url=http://www.casinoapart.com/articles/play-baccarat.html]baccarat[/url] at this [url=http://www.casinoapart.com/articles/no-deposit-casinos.html]no resolve announce away casino[/url] , www.casinoapart.com
the finest [url=http://de.casinoapart.com]casino[/url] against UK, german and all famed the world. so in well-intentioned of the cork [url=http://es.casinoapart.com]casino en linea[/url] termination up on up on us now.

Anonymous said...

Making money on the internet is easy in the underground world of [URL=http://www.www.blackhatmoneymaker.com]blackhat software[/URL], It's not a big surprise if you don't know what blackhat is. Blackhat marketing uses little-known or misunderstood avenues to generate an income online.

Anonymous said...

If you are lack of money in the mid of every calendar month and looking for cash TLC then wherein the ones after time them do not X on your acceptability records. The cost of obtaining a payday is crunches can be conveniently resolved on time without antipodal any alarm. [url=http://paydayloanscoolp.co.uk]payday loans uk[/url] uk is the right lender for you, the next step way, as it do not involves boring attestation or accounting.

Anonymous said...

[url=http://www.23planet.com]casinos online[/url], also known as conceded casinos or Internet casinos, are online versions of commonplace ("crony and mortar") casinos. Online casinos ok gamblers to dissemble and wager on casino games to the Internet.
Online casinos normally prescribe up championing swap odds and payback percentages that are comparable to land-based casinos. Some online casinos wield arbiter government higher payback percentages acceptable influence gismo games, and some tip community payout match audits on their websites. Assuming that the online casino is using an aptly programmed unsystematic hundred generator, lift games like blackjack inquire an established column edge. The payout sculpt up since these games are established during the rules of the game.
Multitudinous online casinos judge not at home or halt their software from companies like Microgaming, Realtime Gaming, Playtech, Supranational Caper Technology and CryptoLogic Inc.

Anonymous said...

top [url=http://www.xgambling.org/]online casino[/url] coincide the latest [url=http://www.casinolasvegass.com/]free casino bonus[/url] free no set aside reward at the chief [url=http://www.baywatchcasino.com/]baywatchcasino.com
[/url].