Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://savenkov.lvk.cs.msu.su/mc/lect02.pdf
Äàòà èçìåíåíèÿ: Fri Feb 24 01:31:11 2012
Äàòà èíäåêñèðîâàíèÿ: Sat Apr 9 22:23:34 2016
Êîäèðîâêà:

2 ()



· 15 · 3 : 1 (10 ) + 2 ( 5 ) · ·
­ 0..9 ­ , . ; , -1 , ­ 10..19 ( ) ­ , ­ 20 ( , ) +1 ; , +1 =>



· : , · : , · : , ,



( , )














· : , · : , · : , · : , · « ».



( , , )



· · · · · · , -, , (. FutureBus+), , .


SPIN
· () · PathStar (Lucent) · (DeepSpace 1, Cassini, Mars Exploration Rovers, .)









M





!





· :
­ ­ ­ ­ ­ ­ ­ ­ (RTE), (deadlock), (starvation), (assertions).

· :
, , , - , ­ .






( , )


?
· () .. · , ,

· .



· ­ , · ?
­ , ­ (ASM) ­ , , , , ­

(stateless )

­ - , -

, , ...

·



( )
int b) { int compare(int a, int res; if (a < b) { res = } else if (a res = } else { res = } return res; }

0: 1: 2: 3: 4: 5: 6: 7:

­
-1; >= b) { 1; 0;

8*296


3 , |int| = 232, 7 + .



( )
void foo(int a) { int x = 5; if (a < x) { x = x-a; } } void bar(int b) { int x; x = b; }

0: 1: 2: 3: 4: 0: 1: 2:

­

15*2128


4 , |int| = 232, 2 , foo ­ 5 , bar ­ 3.



· , ,
­

· .




0: 1: 2: 3: 4: 5: 6: 7: int compare(int a, int res; if (a < b) { res = } else if (a res = } else { res = } return res; } int b) { -1; >= b) { 1; 0;

8*4*264
res 4

7*3*264
5-



· · ­ ? · . .



#include #include int main(int argc, const char* argv[]) { FILE* f; int c, wordCnt = 0, inWord = 0; if (argc < 2) { printf("Use: %s filename\n", argv[0]); return 1; } f = fopen(argv[1], "r"); if (f == NULL) { printf("Can't open file: %s\n", argv [1]); return 1; } while ((c = fgetc(f)) if (!isspace(c)) if (!inWord) { ++wordCnt; inWord = 1; } } else { inWord = } } printf("Word count: fclose(f); return 0; } != -1) { {

0;

%d\n", wordCnt);



#include #include int main(int argc, const char* argv[]) { FILE* f; int c, wordCnt = 0, inWord = 0; if (argc < 2) { printf("Use: %s filename\n", argv[0]); return 1; } f = fopen(argv[1], "r"); if (f == NULL) { printf("Can't open file: %s\n", argv [1]); return 1; } while ((c = fgetc(f)) if (!isspace(c)) if (!inWord) { ++wordCnt; inWord = 1; } } else { inWord = } } printf("Word count: fclose(f); return 0; } != -1) { {

0;

%d\n", wordCnt);



#include #include int main() { FILE* f; int c, wordCnt = 0, inWord = 0; enum {FALSE, TRUE} P1 = any(); if (P1) { return 1; } f = fopen("sample", "r"); if (f == NULL) { return 1; } while ((c = fgetc(f)) != -1) { if (!isspace(c)) { if (!inWord) { ++wordCnt; inWord = 1; } } else { inWord = 0; } } fclose(f); return 0; }



#include #include int main() { FILE* f; int c, wordCnt = 0, inWord = 0; enum {FALSE, TRUE} P1 = any(); if (P1) { return 1; } f = fopen("sample", "r"); if (f == NULL) { return 1; } while ((c = fgetc(f)) != -1) { if (!isspace(c)) { if (!inWord) { ++wordCnt; inWord = 1; } } else { inWord = 0; } } fclose(f); return 0; }



#include #include int main() { FILE* f; int inWord = 0; enum {FALSE, TRUE} P1 = any(), P2, P3; if (P1) { return 1; } f = fopen("sample", "r"); if (f == NULL) { return 1; } while (P2 = any()) { if (P3 = any()) { if(inWord) inWord = 1; } } else { inWord = 0; } } fclose(f); return 0; }



#include #include int main() { FILE* f; int inWord = 0; enum {FALSE, TRUE} P1 = any(), P2, P3; if (P1) { return 1; } f = fopen("sample", "r"); if (f == NULL) { return 1; } while (P2 = any()) { if (P3 = any()) { if(inWord) inWord = 1; } } else { inWord = 0; } } fclose(f); return 0; }



int main() { enum { FCLOSED, FOPEN, enum { V0, V1 } inWord enum {FALSE, TRUE} P1 = P2, P3; if (P1) { return 1; } if(any()) { fileState = FOPEN; } else { fileState = FERROR; } if (fileState == FERROR return 1; } FERROR} fileState; = V0; any(), while (P2 = any()) { if (P3 = any()) { if (!inWord) { inWord = V1; } } else { inWord = V0; } } fileState = FCLOSED; return 0; }

){



int main() { enum { FCLOSED, FOPEN, FERROR} fileState; if(any()) { fileState = FERROR; } else if (any()) { fileState = FOPEN; while (any()); fileState = FCLOSED; } return 0; }

ENTRY FCLOSED

1

FOPEN FERROR FCLOSED
2

EXIT



· ­ , . · , ­ .



· ­ , . · . · · , :
­ ­ ­ ­



· :
­ ,

, ­ , ­ .




· :



· ( ) · ·



consumer:
#include #include #include #define MSG_LEN 100

consumer

foo BAR

bar FOO

struct msgbuf { long mtype; char mtext[MSG_LEN]; }; int main(int argc, const char* argv[]) { key_t key; int qid, i; struct msgbuf msg; ssize_t size; key = ftok("./mc_lec2_ex3", 1);

if ((qid = msgget(key, IPC_CREAT | 0666)) == -1) { perror("Error creating message queue"); } while (1) { size = msgrcv(qid, &msg, MSG_LEN, 1, 0); for (i = 0; i < size; ++i) { msg.mtext[i] = toupper(msg.mtext[i]); } /* send it back */ if (size > 0) { msgsnd(qid, &msg, size, 0); } } }



producer:
#include #include #include #include #include #define MSG_LEN 100 struct msgbuf { long mtype; char mtext[MSG_LEN + 1]; }; int main(int argc, const char* argv[]) { key_t key; int qid; struct msgbuf msg; ssize_t size; msg.mtype = 1; key = ftok("./mc_lec2_ex3", 1);

producer

consumer

if ((qid = msgget(key, IPC_CREAT | 0666)) == -1) { msgget_error: perror("Error creating message queue"); } while (1) { printf("Your message: "); fgets(msg.mtext, MSG_LEN, stdin); if (msgsnd(qid, &msg, strlen(msg.mtext), 0) == -1) { perror("Error sending"); continue; } size = msgrcv(qid, &msg, MSG_LEN, 1, 0); if (size > 0) { printf("Reply is: %s\n", msg.mtext); } else { printf("No reply\n"); } } }


producer:
· msgsnd msgget; · msgget_error, msgsnd msgrcv; · msgrcv msgsnd .


1
producer:
#include #include #include #include #include #define MSG_LEN 100 struct msgbuf { long mtype; char mtext[MSG_LEN + 1]; }; int main(int argc, const char* argv[]) { key_t key; int qid; struct msgbuf msg; ssize_t size; msg.mtype = 1; key = ftok("./mc_lec2_ex3", 1); if ((qid = msgget(key, IPC_CREAT | 0666)) == -1) { msgget_error: perror("Error creating message queue"); } while (1) { printf("Your message: "); fgets(msg.mtext, MSG_LEN, stdin); if (msgsnd(qid, &msg, strlen(msg.mtext), 0) == -1) { perror("Error sending"); continue; } size = msgrcv(qid, &msg, MSG_LEN, 1, 0); if (size > 0) { printf("Reply is: %s\n", msg.mtext); } else { printf("No reply\n"); } } }


1
producer:
#include #include #include #include #include #define MSG_LEN 100 struct msgbuf { long mtype; char mtext[MSG_LEN + 1]; }; int main(int argc, const char* argv[]) { key_t key; int qid; struct msgbuf msg; ssize_t size; msg.mtype = 1; key = ftok("./mc_lec2_ex3", 1); if ((qid = msgget(key, IPC_CREAT | 0666)) == -1) { msgget_error: perror("Error creating message queue"); } while (1) { printf("Your message: "); fgets(msg.mtext, MSG_LEN, stdin); if (msgsnd(qid, &msg, strlen(msg.mtext), 0) == -1) { perror("Error sending"); continue; } size = msgrcv(qid, &msg, MSG_LEN, 1, 0); if (size > 0) { printf("Reply is: %s\n", msg.mtext); } else { printf("No reply\n"); } } }


1
producer:
#include #include #include #include #include #include if (msgget() == -1) { msgget_error: pass(); } while (1) { pass(); pass(); if (msgsnd() == -1) { pass(); continue; } pass(); if (any()) { pass(); } else { pass(); } } }

int main(int argc, const char* argv[]) {

pass(); pass();


producer:
· msgsnd msgget; · msgget_error, msgsnd msgrcv; · msgrcv msgsnd .


2
producer:
#include #include #include #include #include #define MSG_LEN 100 struct msgbuf { long mtype; char mtext[MSG_LEN + 1]; }; int main(int argc, const char* argv[]) { key_t key; int qid; struct msgbuf msg; ssize_t size; msg.mtype = 1; key = ftok("./mc_lec2_ex3", 1); if ((qid = msgget(key, IPC_CREAT | 0666)) == -1) { msgget_error: perror("Error creating message queue"); } while (1) { printf("Your message: "); fgets(msg.mtext, MSG_LEN, stdin); if (msgsnd(qid, &msg, strlen(msg.mtext), 0) == -1) { perror("Error sending"); continue; } size = msgrcv(qid, &msg, MSG_LEN, 1, 0); if (size > 0) { printf("Reply is: %s\n", msg.mtext); } else { printf("No reply\n"); } } }


2
producer:
#include #include #include #include #include #define MSG_LEN 100 struct msgbuf { long mtype; char mtext[MSG_LEN + 1]; }; int main(int argc, const char* argv[]) { key_t key; int qid; struct msgbuf msg; ssize_t size; msg.mtype = 1; key = ftok("./mc_lec2_ex3", 1); if ((qid = msgget(key, IPC_CREAT | 0666)) == -1) { msgget_error: perror("Error creating message queue"); } while (1) { printf("Your message: "); fgets(msg.mtext, MSG_LEN, stdin); if (msgsnd(qid, &msg, strlen(msg.mtext), 0) == -1) { perror("Error sending"); continue; } size = msgrcv(qid, &msg, MSG_LEN, 1, 0); if (size > 0) { printf("Reply is: %s\n", msg.mtext); } else { printf("No reply\n"); } } }


2
producer:
if (msgget() == -1) { msgget_error: pass(); } while (1) { pass(); pass(); if (msgsnd() == -1) { pass(); continue; } msgrcv(); if (any()) { pass(); } else { pass(); } } }

#include

int main(int argc, const char* argv[]) {

!

pass(); pass();

3


producer:
· msgsnd msgget; · msgget_error, msgsnd msgrcv; · msgrcv msgsnd .


,
int main() { enum {RUNNING, STOPPED} state; state = RUNNING; state = STOPPED; }

RUNNING STOPPED



· : 1.


2.
,

3.


! ?