OS PyramidTechnion 234123 · Operating Systemsbasics → exam
Stage 3: IPC
2019B_Spring_AQuestion 1core25 pts

This question spans 2 stages — each part below is tagged with, and links to, the stage it belongs to.

תזכורת: בכל הקצאה של מזהה קובץ חדש (file descriptor) נבחר המזהה הפנוי הקטן ביותר. נתונה התוכנית הבאה: void* func1(void* param){ printf("A"); return 0; } void* func2(void* param){ printf("B"); return 0; } int main(){ pthread_t th; if(fork() > 0){ pthread_create(&th, NULL, func1, NULL); wait(NULL); }else{ close(1); open("file.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666); pthread_create(&th, NULL, func2, NULL); } printf("C"); pthread_join(th,NULL); return 0; } הניחו שכל קריאות המערכת והפונקציות עובדות באופן תקין כצפוי. עתה מוחקים 3 שורות (12,16,22) ומוסיפים במקומם את שורות 13 ו-19. void* func1(void* param){ printf("A"); return 0; } void* func2(void* param){ printf("B"); return 0; } int main(){ pthread_t th; if(fork() > 0){ pthread_create(&th, NULL, func1, NULL); printf("A"); // ADDED wait(NULL); }else{ close(1); open("file.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666); pthread_create(&th, NULL, func2, NULL) pthread_join(th, NULL); // ADDED } printf("C"); pthread_join(th, NULL); return 0; } הניחו שכל קריאות המערכת והפונקציות עובדות באופן תקין כצפוי. הקדמה: מנגנון סנכרון חדש כפי שלמדתם בתרגול, pipe הינו מנגנון חוסם, המאפשר תקשורת בין שני תהליכים. נרצה לנצל את יכולותיו של ה-pipe על מנת ליצור מנעול בין תהליכים. מנעול זה בא לסייע עם race conditions בין תוכניות שונות, למשל על מנת לתקוף את הבעיות בתוכניות הקודמות בשאלה זו, או הבעיות שתראו בשאלה 4. נציג ממשק API למנעול: init_lock, lock, unlock השלימו את המימוש של פונקציות ממשק המנעול, בהנחה שהוא מאותחל במצב לא נעול. char buff[1]; int my_pipe[2]; void init_lock(){ pipe(my_pipe); //? } void lock(){ //? } void unlock(){ //? } לפי אותו הגיון הסטודנט מעוניין לממש סמפור (semaphore) שמאותחל ל-N. הערה: ניתן להניח ש- N קטן מגודל חוצץ ה-4K) pipe). char buff[N]; int my_pipe[2]; int c = N; void init_sem(){ pipe(my_pipe); //? } void sem_wait(){ //? } void sem_signal(){ //? }

Full original question text (raw OCR)

שאלה 1 – טבלת הקבצים וסנכרון (25 נק') – 9 סעיפים

  1. 1· mcq· 3 ptsIPC

    אילו מנגנונים/אובייקטים יכולים לשמש לתקשורת בין תהליכים (IPC)? א. סיגנלים (signals) ופסיקות (interrupts) ב. FIFOS ופסיקות (interrupts) ג. סיגנלים (signals) ומשתנים גלובליים (global variables) ד. Pipes וזיכרון משותף (shared memory) ה. זיכרון משותף (shared memory) ומשתנים גלובליים (global variables) ו. Pipes ומשתנים גלובליים (global variables)

    libc syscall wrapper caching pitfallsLocal vs global interrupt disableSignal delivery and handler timingPipe IPC semanticsSRT / preemptive Gantt construction
  2. הקיפו את התשובה המבטאת את כל אופציות ההדפסה למסך של קוד זה: א. ABCC או BACC ב. CA או AC ג. BAC או ABC ד. ABCC או BCAC או BACC ה. CCA או CAC ו. AC

    System call trap and kernel entryPer-process file descriptor tableHard link vs symlink inode behavior
  3. הקיפו את התשובה המבטאת את כל אופציות ההדפסה למסך של קוד זה: א. ABCC או BACC ב. CA או AC ג. BAC או ABC ד. ABCC או BCAC או BACC ה. CCA או CAC ו. AC

    System call trap and kernel entryPer-process file descriptor tableHard link vs symlink inode behavior
  4. 4· mcq· 2 ptsIPC

    בחרו שורת קוד עבור שורה 6: א. write(my_pipe[1], “m”, 1); ב. read(my_pipe[0], buff, 1); ג. while(read(my_pipe[0], buff, 1)); ד. while(write(my_pipe[1], “m”, 1)); ה. buff[0] = 'm'; ו. שורה ריקה

    Pipe IPC semantics
  5. 5· mcq· 2 ptsIPC

    בחרו שורת קוד עבור שורה 9: א. write(my_pipe[1], “m”, 1); ב. read(my_pipe[0], buff, 1); ג. while(read(my_pipe[0], buff, 1)); ד. while(write(my_pipe[1], “m”, 1)); ה. buff[0] = ''; ו. שורה ריקה

    Pipe IPC semantics
  6. 6· mcq· 2 ptsIPC

    בחרו שורת קוד עבור שורה 12: א. write(my_pipe[1], “m”, 1); ב. read(my_pipe[0], buff, 1); ג. while(read(my_pipe[0], buff, 1)); ד. while(write(my_pipe[1], “m”, 1)); ה. buff[0] = 'm'; ו. שורה ריקה

    Pipe IPC semantics
  7. 7· mcq· 2 ptsIPC

    בחרו שורת קוד עבור שורה 7: א. read(my_pipe[0], buff, 1); ב. while(c--) read(my_pipe[0], buff, 1); ג. read(my_pipe[0], buff, --c); ד. write(my_pipe[1], “m”, 1); ה. while(c--) write(my_pipe[1], “m”, 1); ו. שורה ריקה

    Pipe IPC semantics
  8. 8· mcq· 2 ptsIPC

    בחרו שורת קוד עבור שורה 10: א. read(my_pipe[0], buff, 1); ב. while(c--) read(my_pipe[0], buff, 1); ג. read(my_pipe[0], buff, --c); ד. write(my_pipe[1], “m”, 1); ה. while(c--) write(my_pipe[1], “m”, 1); ו. שורה ריקה

    Pipe IPC semantics
  9. 9· mcq· 2 ptsIPC

    בחרו שורת קוד עבור שורה 13: א. read(my_pipe[0], buff, 1); ב. while(c--) read(my_pipe[0], buff, 1); ג. read(my_pipe[0], buff, --c); ד. write(my_pipe[1], “m”, 1); ה. while(c--) write(my_pipe[1], “m”, 1); ו. שורה ריקה

    Pipe IPC semantics

The exam question — original PDF

pages 2, 3, 4, 5

Exactly as it appears on the exam paper.

loading page 2
loading page 3
loading page 4
loading page 5

Built from these components

Ordered basic → advanced. Master the earlier ones first.

L2System call trap and kernel entryL3libc syscall wrapper caching pitfallsL3Local vs global interrupt disableL3Signal delivery and handler timingL3Per-process file descriptor tableL3Pipe IPC semanticsL3Hard link vs symlink inode behaviorL4SRT / preemptive Gantt construction

Review the material

Read these before you answer — each verified slide teaches a component this question tests, and nothing from an unrelated topic is included. Tutorial slides show the actual slide image.

Lecture 11–12slide 9POSIX file descriptors (FDs)

POSIX file descriptors (FDs) • A successful open<“file name”> of a file returns a FD rpt – A nonnegative integer – An index to a per-process array called the “file descriptor table” – Each entry in the array saves, e.g., the current offset – Threads share the array (and hence the offset) – C’s FILE structure encapsulates a FD • FD or filename? – Some file-related POSIX system calls operate on FDs • read, write, fchmod, fchown, fchdir, fstat, ftruncate… – Others operate on file names • chmod, chown, chdir, stat, truncate – Has security implications: FD versions are more secure in some sense • Because association of FD to underlying file is immutable – Once an FD exists, it will always point to the same file • Whereas association between file & its name is mutable – So they can lead to TOCTTOU (time of check to time of use) races OS (234123) - files 9

Lecture slide — text above is the material (no raster available).
Lecture 11–12slide 26Hard links – when is a file deleted?

Hard links – when is a file deleted? • Every file has a “reference count” associated with it – link()  ref_count++ – unlink()  ref_count-- • if( ref_count == 0 ) – The file has no more names – It isn’t pointed to from any node within the file hierarchy – So it can finally be deleted • What if an open file is deleted? (its ref_count==0) – Can we still access the file through the open FD(s)? • Yes – If >=1 processes have the file open when the last link is removed • The link shall be removed before unlink() returns • But the removal of the file contents shall be postponed until all references (file descriptors) to the file are close()-ed • Have you seen files names that begin with “.nfs”? OS (234123) - files 26

Lecture slide — text above is the material (no raster available).
Lecture 11–12slide 34inodes & *stat syscalls

inodes & *stat syscalls • lstat(2) – Exactly the same as stat(2) if applied to a hard link – But if applied to a symlink, would return the information of this symlink (not to the target of the symlink) – In this case, POSIX says that the only fields within the stat structure that you can portably use are: • st_mode which will specify that the file is a symlink • st_size symlink content length (= length of target filepath) – The value of the rest of the fields could be valid, but it is not specified by POSIX – Notably, it is not specified if a symlink has a corresponding inode • Will be discussed shortly OS (234123) - files 34

Lecture slide — text above is the material (no raster available).
Tutorial 2slide 22קריאות המערכת getpid(), getppid()pid_t getpid();קריאת מערכת המחזירה לתהליך הקורא את ה-pid של עצמו

קריאות המערכת getpid(), getppid()pid_t getpid();קריאת מערכת המחזירה לתהליך הקורא את ה-pid של עצמו.pid_t getppid();קריאת מערכת המחזירה את ה-PID של תהליך האב של התהליך הקורא.שאלה: מה המשמעות של getppid() == 1 עבור תהליך משתמש טיפוסי?תשובה: תהליך האב הוא init. קורה למשל אם תהליך הבן יתום.מערכות הפעלה - תרגול 222

Tutorial 2slide 28מערכות הפעלה - תרגול 2281שאלה ממבחן

מערכות הפעלה - תרגול 2281שאלה ממבחן

Tutorial 3slide 8קריאת המערכת kill#include <sys/types

קריאת המערכת kill#include <sys/types.h>#include <signal.h>int kill(pid_t pid, int sig);פעולה: שולחת את הסיגנל שמספרו sig לתהליך המזוהה ע"י pid.אם הערך של sigהוא 0, אז הפעולה רק בודקת שהתהליך pid קיים, מבלי לשלוח signal (שימושי לבדיקת תקפות pid).ערך מוחזר:0 בהצלחה-1 בכישלון (למשל, אם אין תהליך בעל מזהה pid)מערכות הפעלה - תרגול 38

Tutorial 3slide 9העברת סיגנלים בשני שלביםרישום – מערכת ההפעלה רושמת ב-PCB של תהליך היעד שיש לו סיגנל ממתין (pending signal)

העברת סיגנלים בשני שלביםרישום – מערכת ההפעלה רושמת ב-PCB של תהליך היעד שיש לו סיגנל ממתין (pending signal).הרישום מתבצע במערך בינארי בין 31 ביטים, ולכן לכל תהליך יכול להיות לכל היותר סיגנל ממתין אחד מכל מספר.טיפול – בכל פעם שהתהליך חוזר ממצב גרעין למצב משתמש, מערכת ההפעלה בודקת אם יש סיגנלים ממתינים ומטפלת בהם.בסיום הטיפול בסיגנל, מערכת ההפעלה תאפס את הביט המתאים במערך. במידה ויש מספר סיגנלים ממתינים, סדר הטיפול מתחילת המערך לסופו.מערכות הפעלה - תרגול 39

Tutorial 3slide 19Everything is a file!מערכות הפעלה - תרגול 319keyboardfile objectmonitorfile objectPCBfiles->fdFDT0 (STDIN)1 (STDOUT)2...

Everything is a file!מערכות הפעלה - תרגול 319keyboardfile objectmonitorfile objectPCBfiles->fdFDT0 (STDIN)1 (STDOUT)2 (STDERR)34…textfile objectpipe object

Tutorial 3slide 20FD (file descriptors)כל פעולות קלט/פלט של תהליך בלינוקס מבוצעות דרך "קבצים":קבצים "רגילים" לאחסון מידע (/usr/file

FD (file descriptors)כל פעולות קלט/פלט של תהליך בלינוקס מבוצעות דרך "קבצים":קבצים "רגילים" לאחסון מידע (/usr/file.txt) נמצאים בדיסק.התקני חומרה גם כן מיוצגים כקבצים, אבל נמצאים בזיכרון.למשל, העכברים המחוברים למחשב מיוצגים כ- /dev/input/mouseN .גם ערוצי תקשורת כמו pipes מיוצגים ע"י קבצים שנמצאים בזיכרון.הקשר בין תהליך לבין קובץ שהוא ניגש אליו נשמר, ברמת המשתמש, ע"י מספר שלם שנקרא file descriptor (FD).לדוגמה: קריאת המערכת open() מחזירה FD.המשתמש מעביר את ה-FD לקריאות מערכת כמו read(), write() כדי לקרוא ולכתוב לקובץ.מערכות הפעלה - תרגול 320

Tutorial 3slide 42שחרור file objectשאלה: מי מבצע את שחרור הזיכרון של file object? מתי ניתן לשחררו? ייתכנו מצבים בהם תהליכים שונים מצביע...

שחרור file objectשאלה: מי מבצע את שחרור הזיכרון של file object? מתי ניתן לשחררו? ייתכנו מצבים בהם תהליכים שונים מצביעים לאותו file object, לכן שחרור ה-file object יכול להתבצע רק לאחר ביצוע close() מכל התהליכים החולקים את אותו ה-file object. זכרו של-file object יש מונה (f_count) הסופר את כמות התהליכים המצביעים עליו בכל רגע נתון. המונה קטן באחד עם כל פעולת close() על האובייקט. כאשר המונה מתאפס, ה-file object ישוחרר.מערכות הפעלה - תרגול 342

Tutorial 7slide 14יצירת חוט חדשפרמטרים:thread – מצביע למקום בו יאוחסן מזהה החוט החדש במקרה של סיום הפונקציה בהצלחה

יצירת חוט חדשפרמטרים:thread – מצביע למקום בו יאוחסן מזהה החוט החדש במקרה של סיום הפונקציה בהצלחה.attr – מאפיינים המתארים את תכונות החוט החדש, כגון האם החוט הוא חוט גרעין או חוט משתמש, האם ניתן לבצע לו join, כלומר להמתין לסיומו, וכו'. בד"כ נספק ערך NULL המציין חוט ברירת המחדל של המערכת, שניתן להמתין לסיומו.void* (*start_routine)(void*) מצביע לפונקציה שתהווה את קוד החוט. הערך המוחזר מפונקציה זו במקרה של סיומה הטבעי הינו ערך הסיום של החוט.arg – פרמטר שיסופק לפונקציה עם הפעלתה.מערכות הפעלה - תרגול 714

Tutorial 8slide 12שחרור חוטים ממתיניםint pthread_cond_signal(pthread_cond_t *cond); משחררת את אחד החוטים הממתינים (הגינות לא מובטחת)

שחרור חוטים ממתיניםint pthread_cond_signal(pthread_cond_t *cond); משחררת את אחד החוטים הממתינים (הגינות לא מובטחת).int pthread_cond_broadcast(pthread_cond_t *cond);משחררת את כל החוטים הממתינים.כל החוטים מפסיקים להמתין על משתנה התנאי ועוברים להמתין על המנעול. החוטים יחזרו לפעילות בזה אחר זה (בסדר כלשהו, לאו דווקא הוגן) לאחר שינעלו מחדש את ה-mutex.שימו לב: אם אין אף חוט שממתין באותו רגע על משתנה התנאי cond, הפעולות חסרות השפעה (הסיגנל הולך לאיבוד ואינו נזכר הלאה).ערך מוחזר: הפונקציות תמיד מצליחות ומחזירות 0.מערכות הפעלה - תרגול 812

Tutorial 13slide 13בלינוקס יש שני סוגי קישורים (links)soft / symbolic linkln -s src dstקישור סימבולי הוא קובץ חדש עם inode נפרד מזה של ה...

בלינוקס יש שני סוגי קישורים (links)soft / symbolic linkln -s src dstקישור סימבולי הוא קובץ חדש עם inode נפרד מזה של הקובץ המקורי.כתיבה דרך הקישור כותבת לקובץ אליו הוא מצביע.מחיקת הקישור (באמצעות הפקודה rm) לא תמחק את הקובץ המוצבע.אפשר ליצור קישורים סימבוליים גם לקובץ שלא קיים.hard linkln src dstקישור קשיח הוא שם נרדף לקובץ המקורי כי הוא מצביע ישירות ל-inode של הקובץ המקורי.כתיבה דרך הקישור כותבת לקובץ אליו הוא מצביע.מחיקת הקישור תקטין את מונה הקישורים של הקובץ (כפי שנשמר ב-inode).הקובץ יימחק מהדיסק רק כאשר כל ה-hard links אליו יימחקו.מערכות הפעלה - תרגול 1213

Tutorial 13slide 19>> rm /A/helloמערכות הפעלה - תרגול 1219inode #2type=dirdatanameinode #A5B7inode #5type=dirdatanameinode #…………inode #1...

>> rm /A/helloמערכות הפעלה - תרגול 1219inode #2type=dirdatanameinode #A5B7inode #5type=dirdatanameinode #…………inode #13type=soft_linkdatainode #7type=dirdatanameinode #soft13……data block/A/helloקישור "שבור"!dangling link

Ask Gemini
2019B_Spring_A · Q1 — question + its material already loaded
Pick a shortcut above or ask anything about this question.
The exam text, the skills it tests, and the exact slides are already in context.