ﺑﻪ ﻧﺎﻡ ﺧﺪﺍ ﭘﺮﻭﮊﻩﻱ ﺩﻭﻡ ﺩﺭﺱ ﺳﻴﺴﺘﻢﻋﺎﻣﻞ ﮐﻨﺘﺮﻝ ﻫﻤﺮﻭﻧﺪﻱ ﭘﺮﺩﺍﺯﻩﻫﺎ ﺷﺮﺡ Semaphore API ﭼﻮﻥ ﺩﺭ ﺍﻳﻦ ﭘﺮﻭﮊﻩ ﻧﻴﺎﺯ ﺑﻪ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺳﻤﺎﻓﻮﺭﻫﺎ ﺩﺍﺭﻳﺪ ﻭ system callﻫﺎﻱ Linuxﮐﻪ ﺑـﺮﺍﻱ ﮐـﺎﺭ ﺑـﺎ ﺳـﻤﺎﻓﻮﺭﻫﺎ ﺗﻮﻟﻴـﺪ ﺷﺪﻩﺍﻧﺪ ﺩﺍﺭﺍﻱ ﺟﺰﺋﻴﺎﺕ ﺯﻳﺎﺩﻱ ﻫﺴﺘﻨﺪ ،ﻳﮏ APIﺳﺎﺩﻩﺗﺮ ﺑـﺮﺍﻱ ﮐـﺎﺭ ﺑـﺎ ﺳـﻤﺎﻓﻮﺭﻫﺎ ﺩﺭ ﻗﺎﻟـﺐ ﻓﺎﻳﻠﻬـﺎﻱ Sem.hﻭ Sem.c ﺑﺮﺍﻱ ﺷﻤﺎ ﺗﻮﻟﻴﺪ ﺷﺪﻩ ﻭ ﺑﺎﻳﺪ ﺍﺯ ﺁﻥ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ .ﺷﺮﺡ ﺗﻮﺍﺑﻌﻲ ﮐﻪ ﺍﻳﻦ APIﺩﺭ ﺍﺧﺘﻴﺎﺭ ﺷﻤﺎ ﻣﻲﮔﺬﺍﺭﺩ ﺑﻪ ﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ: )int init_semaphores(int nsems ﺑﺮﺍﻱ ﮐﺎﺭ ﺑﺎ ﺳﻤﺎﻓﻮﺭﻫﺎ ﺑﺎﻳﺪ ﺍﺑﺘﺪﺍ ﺍﻳﻦ ﺗﺎﺑﻊ ﺭﺍ ﻓﺮﺍﺧﻮﺍﻧﻲ ﮐﻨﻴﺪ nsems .ﺗﻌﺪﺍﺩ ﺳﻤﺎﻓﻮﺭﻫﺎﻳﻲ ﺍﺳﺖ ﮐﻪ ﺩﺭ ﺑﺮﻧﺎﻣـﻪ ﺧـﻮﺩ ﺑـﻪ ﺁﻧﻬـﺎ ﻧﻴﺎﺯ ﺩﺍﺭﻳﺪ .ﺩﺭ ﺻﻮﺭﺗﻴﮑﻪ ﺧﻄﺎﻳﻲ ﺭﺥ ﺩﻫﺪ ﭘﻴﺎﻡ ﻣﻨﺎﺳﺐ ﭼﺎﭖ ﺷـﺪﻩ ﻭ ﺧﺮﻭﺟـﻲ -1ﺧﻮﺍﻫـﺪ ﺑـﻮﺩ ﻭ ﺩﺭ ﻏﻴـﺮ ﺍﻳـﻦ ﺻـﻮﺭﺕ ﺧﺮﻭﺟﻲ ﺻﻔﺮ ﺍﺳﺖ .ﭘﺲ ﺍﺯ ﺍﺟﺮﺍﻱ ﺍﻳﻦ ﺗﺎﺑﻊ ﺳﻤﺎﻓﻮﺭﻫﺎﻳﻲ ﺑﺎ ﺷﻤﺎﺭﻩ 0ﺗﺎ nsems-1ﺑﺮﺍﻱ ﺷﻤﺎ ﺗﻮﻟﻴﺪ ﻣﻴﺸﻮﻧﺪ. )int sem_init(int snum, int value ﺍﺯ ﺍﻳﻦ ﺗﺎﺑﻊ ﺑﺮﺍﻱ ﻣﻘﺪﺍﺭ ﺍﻭﻟﻴﻪ ﺩﺍﺩﻥ ﺑﻪ ﺳﻤﺎﻓﻮﺭﻫﺎ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ snum .ﺷﻤﺎﺭﻩ ﺳﻤﺎﻓﻮﺭﻱ ﺍﺳﺖ ﮐﻪ ﻣﻲﺧﻮﺍﻫﻴـﺪ ﺑـﻪ ﺁﻥ ﻣﻘـﺪﺍﺭ ﺍﻭﻟﻴﻪ ﺩﻫﻴﺪ ﻭ valueﻣﻘﺪﺍﺭ ﺍﻭﻟﻴﻪ ﻣﻮﺭﺩ ﻧﻈﺮ ﻣﻲﺑﺎﺷﺪ .ﺩﺭ ﺻﻮﺭﺗﻴﮑﻪ ﺧﻄﺎﻳﻲ ﺭﺥ ﺩﻫﺪ ﭘﻴﺎﻡ ﻣﻨﺎﺳﺐ ﭼﺎﭖ ﺷﺪﻩ ﻭ ﺧﺮﻭﺟﻲ -1 ﺧﻮﺍﻫﺪ ﺑﻮﺩ ﻭ ﺩﺭ ﻏﻴﺮ ﺍﻳﻦ ﺻﻮﺭﺕ ﺧﺮﻭﺟﻲ ﺻﻔﺮ ﺍﺳﺖ. )int sem_signal(int snum ﺩﺳﺘﻮﺭ Signalﺭﺍ ﺑﺮ ﺭﻭﻱ ﺳﻤﺎﻓﻮﺭ ﺑﺎ ﺷﻤﺎﺭﻩ snumﺍﺟﺮﺍ ﻣﻲﮐﻨﺪ .ﺩﺭ ﺻﻮﺭﺗﻴﮑﻪ ﺧﻄﺎﻳﻲ ﺭﺥ ﺩﻫﺪ ﭘﻴﺎﻡ ﻣﻨﺎﺳﺐ ﭼـﺎﭖ ﺷـﺪﻩ ﻭ ﺧﺮﻭﺟﻲ -1ﺧﻮﺍﻫﺪ ﺑﻮﺩ ﻭ ﺩﺭ ﻏﻴﺮ ﺍﻳﻦ ﺻﻮﺭﺕ ﺧﺮﻭﺟﻲ ﺻﻔﺮ ﺍﺳﺖ. )int sem_wait(int snum ﺩﺳﺘﻮﺭ Waitﺭﺍ ﺑﺮ ﺭﻭﻱ ﺳﻤﺎﻓﻮﺭ ﺑﺎ ﺷﻤﺎﺭﻩ snumﺍﺟﺮﺍ ﻣﻲﮐﻨﺪ .ﺩﺭ ﺻﻮﺭﺗﻴﮑﻪ ﺧﻄﺎﻳﻲ ﺭﺥ ﺩﻫﺪ ﭘﻴﺎﻡ ﻣﻨﺎﺳﺐ ﭼـﺎﭖ ﺷـﺪﻩ ﻭ ﺧﺮﻭﺟﻲ -1ﺧﻮﺍﻫﺪ ﺑﻮﺩ ﻭ ﺩﺭ ﻏﻴﺮ ﺍﻳﻦ ﺻﻮﺭﺕ ﺧﺮﻭﺟﻲ ﺻﻔﺮ ﺍﺳﺖ. )(int clean_semaphores ﺗﻤﺎﻣﻲ ﺳﻤﺎﻓﻮﺭﻫﺎ ﺭﺍ ﺁﺯﺍﺩ ﻣﻲﮐﻨﺪ .ﺩﺭ ﺻﻮﺭﺗﻴﮑﻪ ﺧﻄﺎﻳﻲ ﺭﺥ ﺩﻫﺪ ﭘﻴﺎﻡ ﻣﻨﺎﺳﺐ ﭼﺎﭖ ﺷﺪﻩ ﻭ ﺧﺮﻭﺟﻲ -1ﺧﻮﺍﻫـﺪ ﺑـﻮﺩ ﻭ ﺩﺭ ﻏﻴﺮ ﺍﻳﻦ ﺻﻮﺭﺕ ﺧﺮﻭﺟﻲ ﺻﻔﺮ ﺍﺳﺖ. ﺷﺮﺡ Shared Integers API ﺑﺎ ﺗﻮﺟﻪ ﺑﻪ ﺍﻳﻨﮑﻪ ﻣﻤﮑﻦ ﺍﺳﺖ ﺩﺭ ﺣﻴﻦ ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﭘﺮﻭﮊﻩ ﻧﻴﺎﺯ ﺑﻪ ﻣﺘﻐﻴﺮﻫﺎﻱ ﻣﺸﺘﺮﮎ ﺑﻴﻦ ﭘﺮﺩﺍﺯﻩﻫﺎﻱ ﺧﻮﺩ ﺩﺍﺷﺘﻪ ﺑﺎﺷﻴﺪ ،ﻳـﮏ APIﺳﺎﺩﻩ ﺩﺭ ﻗﺎﻟﺐ ﻓﺎﻳﻠﻬﺎﻱ SHI.hﻭ SHI.cﺑﺮﺍﻱ ﺷﻤﺎ ﺗﻮﻟﻴﺪ ﺷﺪﻩ ﻭ ﻣﻲﺗﻮﺍﻧﻴﺪ ﺍﺯ ﺁﻥ ﺑﺮﺍﻱ ﺩﺍﺷﺘﻦ ﻣﺘﻐﻴﺮﻫﺎﻱ ﻣﺸـﺘﺮﮎ ﺍﺯ ﻧﻮﻉ intﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ )ﺩﺭ ﺻﻮﺭﺗﻴﮑﻪ ﻧﻴﺎﺯ ﺑﻪ ﺩﺍﺷﺘﻦ ﻣﺘﻐﻴﺮﻫﺎﻱ ﻣﺸﺘﺮﮐﻲ ﺑﺎ ﻧﻮﻉ ﺩﻳﮕﺮ ﺩﺍﺭﻳـﺪ ﻣـﻲﺗﻮﺍﻧﻴـﺪ ﻣﺴـﺘﻘﻴﻤﺎ ﺍﺯ ﺍﻣﮑﺎﻧـﺎﺕ Shared Memoryﺩﺭ Linuxﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ( .ﺷﺮﺡ ﺗﻮﺍﺑﻌﻲ ﮐﻪ ﺍﻳﻦ APIﺩﺭ ﺍﺧﺘﻴﺎﺭ ﺷـﻤﺎ ﻣـﻲﮔـﺬﺍﺭﺩ ﺑـﻪ ﺻـﻮﺭﺕ ﺯﻳـﺮ ﺍﺳﺖ: )int init_integers(int nints ﺑﺮﺍﻱ ﮐﺎﺭ ﺑﺎ ﺍﻋﺪﺍﺩ ﺻﺤﻴﺢ ﻣﺸﺘﺮﮎ ﺑﺎﻳﺪ ﺍﺑﺘﺪﺍ ﺍﻳﻦ ﺗﺎﺑﻊ ﺭﺍ ﻓﺮﺍﺧﻮﺍﻧﻲ ﮐﻨﻴﺪ nints .ﺗﻌﺪﺍﺩ ﺍﻋﺪﺍﺩ ﺻﺤﻴﺢ ﻣﺸـﺘﺮﮐﻲ ﺍﺳـﺖ ﮐـﻪ ﺩﺭ ﺑﺮﻧﺎﻣﻪ ﺧﻮﺩ ﺑﻪ ﺁﻧﻬﺎ ﻧﻴﺎﺯ ﺩﺍﺭﻳﺪ .ﺩﺭ ﺻﻮﺭﺗﻴﮑﻪ ﺧﻄﺎﻳﻲ ﺭﺥ ﺩﻫﺪ ﭘﻴﺎﻡ ﻣﻨﺎﺳﺐ ﭼﺎﭖ ﺷﺪﻩ ﻭ ﺧﺮﻭﺟﻲ -1ﺧﻮﺍﻫﺪ ﺑﻮﺩ ﻭ ﺩﺭ ﻏﻴـﺮ ﺍﻳﻦ ﺻﻮﺭﺕ ﺧﺮﻭﺟﻲ ﺻﻔﺮ ﺍﺳﺖ .ﭘﺲ ﺍﺯ ﺍﺟﺮﺍﻱ ﺍﻳﻦ ﺗﺎﺑﻊ ﺍﻋﺪﺍﺩ ﺻﺤﻴﺤﻲ ﺑﺎ ﺷﻤﺎﺭﻩﻫﺎﻱ 0ﺗـﺎ nints-1ﺑـﺮﺍﻱ ﺷـﻤﺎ ﺗﻮﻟﻴـﺪ ﻣﻲﺷﻮﺩ. )void shi_set(int inum, int value ﺍﺯ ﺍﻳﻦ ﺗﺎﺑﻊ ﺑﺮﺍﻱ ﺗﻨﻈﻴﻢ ﻣﻘﺪﺍﺭ ﻋﺪﺩ ﺻﺤﻴﺢ ﻣﺸﺘﺮﮎ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ snum .ﺷﻤﺎﺭﻩ ﻋﺪﺩ ﺻﺤﻴﺤﻲ ﺍﺳﺖ ﮐﻪ ﻣﻲﺧﻮﺍﻫﻴـﺪ ﺑـﻪ ﺁﻥ ﻣﻘﺪﺍﺭ ﺟﺪﻳﺪ ﺑﺪﻫﻴﺪ ﻭ valueﻣﻘﺪﺍﺭ ﻣﻮﺭﺩ ﻧﻈﺮ ﻣﻲﺑﺎﺷﺪ. )int shi_get(int inum ﺍﺯ ﺍﻳﻦ ﺗﺎﺑﻊ ﺑﺮﺍﻱ ﮔﺮﻓﺘﻦ ﻣﻘﺪﺍﺭ ﻋﺪﺩ ﺻﺤﻴﺢ ﻣﺸﺘﺮﮎ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ snum .ﺷﻤﺎﺭﻩ ﻋﺪﺩ ﺻﺤﻴﺤﻲ ﺍﺳﺖ ﮐﻪ ﻣﻲﺧﻮﺍﻫﻴﺪ ﺑـﻪ ﺁﻥ ﻣﻘﺪﺍﺭ ﺟﺪﻳﺪ ﺑﺪﻫﻴﺪ ﻭ ﺧﺮﻭﺟﻲ ﺗﺎﺑﻊ ﻣﻘﺪﺍﺭ ﻋﺪﺩ ﺻﺤﻴﺢ ﺍﺳﺖ. )(int clean_integers ﻓﻀﺎﻱ ﺍﺧﺘﺼﺎﺹ ﺩﺍﺩﻩ ﺷﺪﻩ ﺑﺮﺍﻱ ﺍﻋﺪﺍﺩ ﺻﺤﻴﺢ ﻣﺸﺘﺮﮎ ﺭﺍ ﺁﺯﺍﺩ ﻣﻲﮐﻨﺪ .ﺩﺭ ﺻﻮﺭﺗﻴﮑﻪ ﺧﻄﺎﻳﻲ ﺭﺥ ﺩﻫـﺪ ﭘﻴـﺎﻡ ﻣﻨﺎﺳـﺐ ﭼـﺎﭖ ﺷﺪﻩ ﻭ ﺧﺮﻭﺟﻲ -1ﺧﻮﺍﻫﺪ ﺑﻮﺩ ﻭ ﺩﺭ ﻏﻴﺮ ﺍﻳﻦ ﺻﻮﺭﺕ ﺧﺮﻭﺟﻲ ﺻﻔﺮ ﺍﺳﺖ. ﺷﺮﺡ ﭘﺮﻭﮊﻩ ﻣﺴﺎﺋﻠﻲ ﮐﻪ ﺑﺎﻳﺪ ﺁﻧﻬﺎ ﺭﺍ ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﮐﻨﻴﺪ ﻋﺒﺎﺭﺗﻨﺪ ﺍﺯ: .1ﻣﺴﺌﻠﻪ ﺧﻮﺍﻧﻨﺪﮔﺎﻥ ﻭ ﻧﻮﻳﺴﻨﺪﮔﺎﻥ ) (Readers-Writersﺭﺍ ﺩﺭ ﺣﺎﻟﺘﻴﮑﻪ ﻣﺸﮑﻞ ﮔﺮﺳﻨﮕﻲ ﺑﺮﺍﻱ ﻫـﻴﭻ ﻧﻮﻳﺴـﻨﺪﻩ ﻭ ﺧﻮﺍﻧﻨﺪﻩﺍﻱ ﺭﺥ ﻧﺪﻫﺪ ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﮐﻨﻴﺪ .ﺗﻮﺟﻪ ﮐﻨﻴﺪ ﮐﻪ ﺩﺭ ﻫﺮ ﻟﺤﻈﻪ ﻳﺎ ﻳﮏ ﻧﻮﻳﺴﻨﺪﻩ ﺩﺭ ﺣﺎﻝ ﻧﻮﺷﺘﻦ ﺍﺳﺖ ﻭ ﻳـﺎ n ﺧﻮﺍﻧﻨﺪﻩ ﺩﺭ ﺣﺎﻝ ﺧﻮﺍﻧﺪﻥ ﮐﻪ n >= 0ﺍﺳﺖ .ﺑﺮﻧﺎﻣﻪ ﺷﻤﺎ ﺑﺎﻳﺪ ﺍﺑﺘﺪﺍ ﭘﺎﺭﺍﻣﺘﺮﻫـﺎﻱ ﻭﺭﻭﺩﻱ rﻭ wﺭﺍ ﺍﺯ ﺧـﻂ ﻓﺮﻣـﺎﻥ ﮔﺮﻓﺘﻪ ﻭ ﺳﭙﺲ ﺑﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺩﺳﺘﻮﺭ forkﺗﻌﺪﺍﺩ rﭘـﺮﺩﺍﺯﻩ ﺧﻮﺍﻧﻨـﺪﻩ ﺑـﺎ ﺷـﻤﺎﺭﻩ ﻫـﺎﻱ 1ﺗـﺎ rﻭ ﺗﻌـﺪﺍﺩ wﭘـﺮﺩﺍﺯﻩ ﻧﻮﻳﺴﻨﺪﻩ ﺑﺎ ﺷﻤﺎﺭﻩ ﻫﺎﻱ 1ﺗﺎ wﺗﻮﻟﻴﺪ ﮐﺮﺩﻩ ﻭ ﺳﭙﺲ ﺁﻧﻬﺎ ﺭﺍ ﺍﺟﺮﺍ ﮐﻨﺪ .ﺍﺯ ﺳﻤﺎﻓﻮﺭﻫﺎ ﺑﺮﺍﻱ ﮐﻨﺘﺮﻝ ﻫﻤﺮﻭﻧﺪﻱ ﭘـﺮﺍﺯﻩ- ﻫﺎ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ. ﻣﻮﺍﺭﺩ ﺯﻳﺮ ﺑﺎﻳﺪ ﺩﻗﻴﻘﺎ ﺩﺭ ﺧﺮﻭﺟﻲ ﺑﺮﻧﺎﻣﻪ ﺷﻤﺎ ﭼﺎﭖ ﺷﻮﻧﺪ: · ﻫﺮﮔﺎﻩ ﺧﻮﺍﻧﻨﺪﻩ ﺑﺎ ﺷﻤﺎﺭﻩ iﺷﺮﻭﻉ ﺑﻪ ﺧﻮﺍﻧﺪﻥ ﮐﺮﺩ: · ﻫﺮﮔﺎﻩ ﺧﻮﺍﻧﺪﻥ ﺧﻮﺍﻧﻨﺪﻩ ﺑﺎ ﺷﻤﺎﺭﻩ iﺗﻤﺎﻡ ﺷﺪ: · ﻫﺮﮔﺎﻩ ﻧﻮﻳﺴﻨﺪﻩ ﺑﺎ ﺷﻤﺎﺭﻩ iﺷﺮﻭﻉ ﺑﻪ ﻧﻮﺷﺘﻦ ﮐﺮﺩ: · ﻫﺮﮔﺎﻩ ﻧﻮﺷﺘﻦ ﻧﻮﻳﺴﻨﺪﻩ ﺑﺎ ﺷﻤﺎﺭﻩ iﺗﻤﺎﻡ ﺷﺪ: R : i : Start R : i : End W : i : Start W : i : End · · · · ﺩﺭ ﺷﮑﻞ ﺯﻳﺮ ﻧﻤﻮﻧﻪﺍﻱ ﺍﺯ ﺧﺮﻭﺟﻲ ﺍﻳﻦ ﺑﺮﻧﺎﻣﻪ ﻧﻤﺎﻳﺶ ﺩﺍﺩﻩ ﺷﺪﻩ ﺍﺳﺖ: .2ﻓﺮﺽ ﮐﻨﻴﺪ ﺗﻌﺪﺍﺩﻱ ﺩﺍﻧﺸﺠﻮ ﺩﺍﺭﻳﻢ ﮐﻪ ﻫﺮ ﻳﮏ ﻣﻲﺧﻮﺍﻫﺪ ﻳﮏ ﺳـﻮﺍﻝ ﺍﺯ ﺍﺳـﺘﺎﺩ ﺧـﻮﺩ ﺑﭙﺮﺳـﺪ .ﺑـﺮﺍﻱ ﺍﻳـﻦ ﻣﻨﻈـﻮﺭ ﺍﺗﻔﺎﻗﺎﺕ ﺯﻳﺮ ﺑﻪ ﺗﺮﺗﻴﺐ ﺭﻭﻱ ﻣﻴﺪﻫﻨﺪ: · ﺩﺍﻧﺸﺠﻮﻱ iﻭﺍﺭﺩ ﺍﺗﺎﻕ ﺍﺳﺘﺎﺩ ﻣﻲ ﺷﻮﺩ. · ﺩﺍﻧﺸﺠﻮﻱ iﺳﻮﺍﻝ ﺧﻮﺩ ﺭﺍ ﻣﻲﭘﺮﺳﺪ. · ﺍﺳﺘﺎﺩ ﺷﺮﻭﻉ ﺑﻪ ﺟﻮﺍﺏ ﺩﺍﺩﻥ ﻣﻴﮑﻨﺪ. · T : Answering to : i ﺟﻮﺍﺏ ﺩﺍﺩﻥ ﺑﻴﻦ 0ﺗﺎ 2ﺛﺎﻧﻴﻪ ﻃﻮﻝ ﻣﻲﮐﺸﺪ! S : i : Entered room S : i : Asked question · ﺍﺳﺘﺎﺩ ﺟﻮﺍﺏ ﺩﺍﺩﻥ ﺭﺍ ﺗﻤﺎﻡ ﻣﻲﮐﻨﺪ. T : Finished answering to : i · · · · · ﺩﺍﻧﺸﺠﻮﻱ iﺗﺸﮑﺮ ﻣﻲﮐﻨﺪ. · ﺩﺍﻧﺸﺠﻮﻱ iﺍﺯ ﺍﺗﺎﻕ ﺧﺎﺭﺝ ﻣﻲﺷﻮﺩ. S : i : Thanks S : i : Exited room · · ﻣﺤﺪﻭﺩﻳﺘﻬﺎﻱ ﺯﻳﺮ ﺩﺭ ﺍﻳﻦ ﻣﺴﺌﻠﻪ ﻭﺟﻮﺩ ﺩﺍﺭﻧﺪ: .aﺩﺭ ﻫﺮ ﻟﺤﻈﻪ ﺣﺪﺍﮐﺜﺮ 3ﺩﺍﻧﺸﺠﻮ ﻣﻲﺗﻮﺍﻧﻨﺪ ﺩﺭ ﺍﺗﺎﻕ ﺍﺳﺘﺎﺩ ﺑﺎﺷﻨﺪ. .bﺍﺳﺘﺎﺩ ﺩﺭ ﻫﺮ ﻟﺤﻈﻪ ﺣﺪﺍﮐﺜﺮ ﺑﻪ ﻳﮏ ﺳﻮﺍﻝ ﭘﺎﺳﺦ ﻣﻲﺩﻫﺪ. ﺑﺮﻧﺎﻣﻪ ﺷﻤﺎ ﺑﺎﻳﺪ ﺍﺑﺘﺪﺍ ﭘﺎﺭﺍﻣﺘﺮ ﻭﺭﻭﺩﻱ nﺭﺍ ﺍﺯ ﺧﻂ ﻓﺮﻣـﺎﻥ ﮔﺮﻓﺘـﻪ ﻭ ﺳـﭙﺲ ﺑـﺎ ﺍﺳـﺘﻔﺎﺩﻩ ﺍﺯ ﺩﺳـﺘﻮﺭ forkﺗﻌـﺪﺍﺩ n ﭘﺮﺩﺍﺯﻩ ﺩﺍﻧﺸﺠﻮ ﺑﺎ ﺷﻤﺎﺭﻩ ﻫﺎﻱ 1ﺗﺎ nﺗﻮﻟﻴﺪ ﮐﺮﺩﻩ ﻭ ﺳﭙﺲ ﺁﻧﻬﺎ ﺭﺍ ﺍﺟﺮﺍ ﮐﻨﺪ .ﺍﺯ ﺳﻤﺎﻓﻮﺭﻫﺎ ﺑﺮﺍﻱ ﮐﻨﺘـﺮﻝ ﻫﻤﺮﻭﻧـﺪﻱ ﭘﺮﺩﺍﺯﻩﻫﺎ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ. ﺩﺭ ﺷﮑﻞ ﺯﻳﺮ ﻧﻤﻮﻧﻪﺍﻱ ﺍﺯ ﺧﺮﻭﺟﻲ ﺍﻳﻦ ﺑﺮﻧﺎﻣﻪ ﻧﻤﺎﻳﺶ ﺩﺍﺩﻩ ﺷﺪﻩ ﺍﺳﺖ: .3ﻓﺮﺽ ﮐﻨﻴﺪ ﺍﺯ ﺷﻤﺎ ﺧﻮﺍﺳﺘﻪ ﺷﺪﻩ ﺍﺳﺖ ﮐﻪ ﺑﺮﻧﺎﻣﻪﺍﻱ ﺑﺮﺍﻱ ﮐﻨﺘﺮﻝ ﻳﮏ ﺁﺳﺎﻧﺴﻮﺭ ﺑﻨﻮﻳﺴﻴﺪ .ﺍﻳﻦ ﺁﺳﺎﻧﺴﻮﺭ ﺑﻴﻦ ﻃﺒﻘﺎﺕ 1ﺗﺎ 4ﺍﺯ ﺳﺎﺧﺘﻤﺎﻧﻲ ﺣﺮﮐﺖ ﻣﻲﮐﻨﺪ ﻭ ﺩﺭ ﺍﺑﺘﺪﺍ ﺩﺭ ﻃﺒﻘـﻪ ﺍﻭﻝ ﻗـﺮﺍﺭ ﺩﺍﺭﺩ .ﺗـﺎ ﺯﻣﺎﻧﻴﮑـﻪ ﺩﺭﺧﻮﺍﺳـﺘﻲ ﻧﺮﺳـﻴﺪﻩ ﺑﺎﺷـﺪ ﺁﺳﺎﻧﺴﻮﺭ ﺑﻲﺣﺮﮐﺖ ﺍﺳﺖ .ﻫﻨﮕﺎﻣﻴﮑﻪ ﺁﺳﺎﻧﺴﻮﺭ ﺩﺭﺧﻮﺍﺳﺘﻲ ﺩﺭﻳﺎﻓﺖ ﮐﻨﺪ ﺷﺮﻭﻉ ﺑﻪ ﺣﺮﮐﺖ ﻣﻴﮑﻨـﺪ .ﻧﺤـﻮﻩ ﺣﺮﮐـﺖ ﺑﻪ ﺍﻳﻦ ﺗﺮﺗﻴﺐ ﺍﺳﺖ ﮐﻪ ﺁﺳﺎﻧﺴﻮﺭ ﺷﺮﻭﻉ ﺑﻪ ﺑﺎﻻ ﺭﻓﺘﻦ ﻣﻴﮑﻨﺪ ﻭ ﺩﺭ ﺣﻴﻦ ﺣﺮﮐﺖ ﺑﻪ ﺩﺭﺧﻮﺍﺳﺘﻬﺎ ﻋﻤﻞ ﻣﻲﮐﻨﺪ .ﻫﺮﮔﺎﻩ ﺁﺳﺎﻧﺴﻮﺭ ﺑﻪ ﻃﺒﻘﻪ ﭼﻬﺎﺭﻡ ﺑﺮﺳﺪ ﻭ ﻫﻨﻮﺯ ﺩﺭﺧﻮﺍﺳﺘﻲ ﻭﺟﻮﺩ ﺩﺍﺷﺘﻪ ﺑﺎﺷﺪ ،ﺟﻬﺖ ﺣﺮﮐﺖ ﺑـﺮﻋﮑﺲ ﺷـﺪﻩ ﻭ ﺣﺮﮐـﺖ ﺍﺩﺍﻣﻪ ﻣﻲﻳﺎﺑﺪ .ﺑﻪ ﺍﻳﻦ ﺗﺮﺗﻴﺐ ﻣﺎﺩﺍﻣﻴﮑﻪ ﺩﺭﺧﻮﺍﺳﺘﻲ ﻭﺟﻮﺩ ﺩﺍﺷﺘﻪ ﺑﺎﺷﺪ ﺁﺳﺎﻧﺴﻮﺭ ﺍﺯ ﻃﺒﻘﻪ 1ﺑﻪ 4ﺭﻓﺘﻪ ﻭ ﺳـﭙﺲ ﺍﺯ 4 ﺑﻪ 1ﺑﺮ ﻣﻲﮔﺮﺩﺩ ﻭ ﺍﻳﻦ ﺣﻠﻘﻪ ﺍﺩﺍﻣﻪ ﻣﻲﻳﺎﺑﺪ .ﺩﺭ ﺿﻤﻦ ﻇﺮﻓﻴﺖ ﺁﺳﺎﻧﺴﻮﺭ ﻧﺎﻣﺤﺪﻭﺩ ﺍﺳﺖ ﻭ ﻫﺮ ﺗﻌﺪﺍﺩ ﺷـﺨﺺ ﻣـﻲ- ﺗﻮﺍﻧﻨﺪ ﻫﻤﺰﻣﺎﻥ ﺳﻮﺍﺭ ﺁﻥ ﺑﺸﻮﻧﺪ .ﺑﻪ ﻋﻨﻮﺍﻥ ﻣﺜﺎﻝ ﻓﺮﺽ ﮐﻨﻴﺪ ﺁﺳﺎﻧﺴﻮﺭ ﺩﺭ ﻃﺒﻘﻪ 3ﺑﺎﺷﺪ ﻭ ﺷﺨﺼﻲ ﺩﺭﺧﻮﺍﺳﺖ ﺧﻮﺩ ﺑﺮﺍﻱ ﺭﻓﺘﻦ ﺍﺯ ﻃﺒﻘﻪ 2ﺑﻪ 4ﺭﺍ ﺑﻪ ﺁﺳﺎﻧﺴﻮﺭ ﺑﺪﻫﺪ .ﺩﺭ ﺍﻳﻦ ﺻﻮﺭﺕ ﺁﺳﺎﻧﺴﻮﺭ ﺑﻪ ﺗﺮﺗﻴﺐ ﺑﻪ ﺍﻳﻦ ﻃﺒﻘﺎﺕ ﻣـﻲﺭﻭﺩ 4 :ﻭ 3ﻭ ) 2ﺷﺨﺺ ﺳﻮﺍﺭ ﻣﻴﺸﻮﺩ( ﻭ 1ﻭ 2ﻭ 3ﻭ ) 4ﺷﺨﺺ ﭘﻴﺎﺩﻩ ﻣﻲﺷﻮﺩ( .ﮔﺮﭼﻪ ﺍﻳﻦ ﻧﻮﻉ ﺣﺮﮐـﺖ ﺑﻬﻴﻨـﻪ ﻧﻴﺴـﺖ ﻭﻟﻲ ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﺁﻥ ﺁﺳﺎﻧﺘﺮ ﺍﺳﺖ. ﺑﺮﻧﺎﻣﻪ ﺷﻤﺎ ﺑﺎﻳﺪ ﺍﺑﺘﺪﺍ ﭘﺎﺭﺍﻣﺘﺮ ﻭﺭﻭﺩﻱ nﺭﺍ ﺍﺯ ﺧﻂ ﻓﺮﻣـﺎﻥ ﮔﺮﻓﺘـﻪ ﻭ ﺳـﭙﺲ ﺑـﺎ ﺍﺳـﺘﻔﺎﺩﻩ ﺍﺯ ﺩﺳـﺘﻮﺭ forkﺗﻌـﺪﺍﺩ n ﭘﺮﺩﺍﺯﻩ Personﺑﺎ ﺷﻤﺎﺭﻩ ﻫﺎﻱ 1ﺗﺎ nﺗﻮﻟﻴﺪ ﮐﺮﺩﻩ ﻭ ﻃﺒﻘﺎﺕ ﻣﺒﺪﺍ ﻭ ﻣﻘﺼﺪ ﻫﺮ ﺷﺨﺺ ﺭﺍ ﺑﺎ ﺗﻮﻟﻴﺪ ﺍﻋﺪﺍﺩ ﻣﺘﻔـﺎﻭﺕ ﺗﺼﺎﺩﻓﻲ ﺑﻴﻦ 1ﺗﺎ 4ﻣﺸﺨﺺ ﮐﺮﺩﻩ ﻭ ﺳﭙﺲ ﺁﻧﻬﺎ ﺭﺍ ﺍﺟﺮﺍ ﮐﻨﺪ .ﺍﺯ ﺳـﻤﺎﻓﻮﺭﻫﺎ ﺑـﺮﺍﻱ ﮐﻨﺘـﺮﻝ ﻫﻤﺮﻭﻧـﺪﻱ ﭘـﺮﺩﺍﺯﻩﻫـﺎ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ. ﻣﻮﺍﺭﺩ ﺯﻳﺮ ﺑﺎﻳﺪ ﺩﻗﻴﻘﺎ ﺩﺭ ﺧﺮﻭﺟﻲ ﺑﺮﻧﺎﻣﻪ ﺷﻤﺎ ﭼﺎﭖ ﺷﻮﻧﺪ: · ﺷﺨﺺ iﺩﺭ ﻃﺒﻘﻪ fﻗﺮﺍﺭ ﺩﺍﺭﺩ ﻭ ﻣﻲﺧﻮﺍﻫﺪ ﺑﻪ ﺻﺒﻘﻪ tﺑﺮﻭﺩ: P : i : Wants to go from f to t · ﺷﺨﺺ iﻭﺍﺭﺩ ﺁﺳﺎﻧﺴﻮﺭ ﺷﺪ: · ﺷﺨﺺ iﺍﺯ ﺁﺳﺎﻧﺴﻮﺭ ﺧﺎﺭﺝ ﺷﺪ: · ﺁﺳﺎﻧﺴﻮﺭ ﻭﺍﺭﺩ ﻃﺒﻘﻪ nﺷﺪ: P : i : Entered elevator P : i : Exited elevator Elevator entered floor n ﺩﺭ ﺷﮑﻞ ﺯﻳﺮ ﻧﻤﻮﻧﻪﺍﻱ ﺍﺯ ﺧﺮﻭﺟﻲ ﺍﻳﻦ ﺑﺮﻧﺎﻣﻪ ﻧﻤﺎﻳﺶ ﺩﺍﺩﻩ ﺷﺪﻩ ﺍﺳﺖ: · · · · ﺑﺮﺍﻱ ﺍﻧﺠﺎﻡ ﺍﻳﻦ ﭘﺮﻭﮊﻩ ﻣﺠﻤﻮﻋﻪ ﻓﺎﻳﻠﻬﺎﻱ ﺯﻳﺮ ﺩﺭ ﻗﺎﻟﺐ ﻓﺎﻳﻞ Prj2.tar.gzﺩﺭ ﺍﺧﺘﻴﺎﺭ ﺷﻤﺎ ﻗﺮﺍﺭ ﮔﺮﻓﺘﻪﺍﻧﺪ: )· Sem.h , Sem.c (Semaphore API )· SHI.h , SHI.c (Shared Integers API · RandSleep.h , RandSleep.c ﺩﺭ ﺍﻳﻦ ﺩﻭ ﻓﺎﻳﻞ ﺗﺎﺑﻊ ) randSleep(int maxsecﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﺷﺪﻩ ﺍﺳﺖ ﮐﻪ ﺍﻳﻦ ﺗﺎﺑﻊ ﺑﻴﻦ 0ﺗﺎ maxsecﺛﺎﻧﻴﻪ ﺗﻮﻗـﻒ ﺩﺭ ﺍﺟﺮﺍﻱ ﺑﺮﻧﺎﻣﻪ ﺷﻤﺎ ﺍﻳﺠﺎﺩ ﻣﻲﮐﻨﺪ )ﺑﺎ ﺩﻗﺖ ﻧﺎﻧﻮﺛﺎﻧﻴﻪ( .ﺍﺯ ﺍﻳﻦ ﺗﺎﺑﻊ ﻣﻲﺗﻮﺍﻧﻴﺪ ﺑﺮﺍﻱ ﺩﺍﺷﺘﻦ ﺧﺮﻭﺟﻴﻬﺎﻱ ﺗﺼﺎﺩﻓﻲﺗﺮ ﺩﺭ ﺑﺮﻧﺎﻣﻪﻫـﺎﻱ ﺧﻮﺩ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ. · Sample.c ﺩﺭ ﺍﻳﻦ ﻓﺎﻳﻞ ﺑﺮﺍﻱ ﺁﺷﻨﺎﻳﻲ ﺑﻴﺸﺘﺮ ﺷﻤﺎ ﺑﺎ APIﻫﺎﻱ ﺗﻮﻟﻴﺪ ﺷﺪﻩ ،ﻣﺴﺌﻠﻪ ﺗﻮﻟﻴﺪﮐﻨﻨﺪﻩ – ﻣﺼﺮﻑ ﮐﻨﻨـﺪﻩ ﺑـﺎ ﺑـﺎﻓﺮ ﻧﺎﻣﺤـﺪﻭﺩ ﭘﻴـﺎﺩﻩ ﺳﺎﺯﻱ ﺷﺪﻩ ﺍﺳﺖ. RW.c ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﻣﺴﺌﻠﻪ ﺍﻭﻝ ﺭﺍ ﺩﺭ ﻗﺎﻟﺐ ﺍﻳﻦ ﻓﺎﻳﻞ ﺑﺎﻳﺪ ﺍﻧﺠﺎﻡ ﺩﻫﻴﺪ. Questions.c ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﻣﺴﺌﻠﻪ ﺩﻭﻡ ﺭﺍ ﺩﺭ ﻗﺎﻟﺐ ﺍﻳﻦ ﻓﺎﻳﻞ ﺑﺎﻳﺪ ﺍﻧﺠﺎﻡ ﺩﻫﻴﺪ. Elevator.c ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﻣﺴﺌﻠﻪ ﺳﻮﻡ ﺭﺍ ﺩﺭ ﻗﺎﻟﺐ ﺍﻳﻦ ﻓﺎﻳﻞ ﺑﺎﻳﺪ ﺍﻧﺠﺎﻡ ﺩﻫﻴﺪ. Makefile · · · · ﺍﺯ ﺍﻳﻦ Makefileﻣﻲﺗﻮﺍﻧﻴﺪ ﺑﺮﺍﻱ ﺗﻮﻟﻴﺪ ﻓﺎﻳﻠﻬﺎﻱ ﺍﺟﺮﺍﻳﻲ ﺑﺮﻧﺎﻣﻪﻫﺎﻱ ﺧﻮﺩ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ. ﻣﻼﺣﻈﺎﺕ: .1ﭘﺮﻭﮊﻩ ﺭﺍ ﺩﺭ ﮔﺮﻭﻫﻬﺎﻱ ﺩﻭ ﻧﻔﺮﻩ ﺍﻧﺠﺎﻡ ﺩﻫﻴﺪ. .2ﺑﻪ ﭘﻴﺎﺩﻩ ﺳﺎﺯﻳﻬﺎﻱ ﻣﺸﺎﺑﻪ ﻧﻤﺮﻩ ﻣﻨﻔﻲ ﺗﻌﻠﻖ ﺧﻮﺍﻫﺪ ﮔﺮﻓﺖ. ﺗﺤﻮﻳﻞ ﭘﺮﻭﮊﻩ: ﺗﺤﻮﻳﻞ ﭘﺮﻭﮊﻩ ﺩﺭ ﺩﻭ ﻣﺮﺣﻠﻪ ﺻﻮﺭﺕ ﺧﻮﺍﻫﺪ ﮔﺮﻓﺖ :ﺩﺭ ﻣﺮﺣﻠﻪ ﺍﻭﻝ ﺑﺎﻳﺪ ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﺧﻮﺩ ﺭﺍ ﺑﻪ ﺻﻮﺭﺕ ﻓﺎﻳـﻞ ﻓﺸـﺮﺩﻩ ﺷـﺪﻩ )ﺑﺎ ﻧـﺎﻡ (StudentNumber1-StudentName1-StudentNumber2-StudentName2.tar.gzﺣـﺪﺍﮐﺜﺮ ﺗـﺎ ﺗﺎﺭﻳﺦ 84/10 /15ﺑﻪ ﺁﺩﺭﺱ ganji@ce.sharif.eduﺍﺭﺳﺎﻝ ﮐﻨﻴﺪ )ﺑﻌـﺪ ﺍﺯ ﺍﻳـﻦ ﺗـﺎﺭﻳﺦ ﺑـﻪ ﺍﺯﺍﻱ ﻫـﺮ ﺭﻭﺯ ﺗـﺎﺧﻴﺮ 20 ﺩﺭﺻﺪ ﺍﺯ ﻧﻤﺮﻩ ﭘﺮﻭﮊﻩ ﺭﺍ ﺍﺯ ﺩﺳﺖ ﺧﻮﺍﻫﻴﺪ ﺩﺍﺩ( .ﺯﻣﺎﻥ ﺗﺤﻮﻳﻞ ﺣﻀﻮﺭﻱ ﭘﺮﻭﮊﻩ ﻣﺘﻌﺎﻗﺒﺎﹰ ﺍﻋﻼﻡ ﺧﻮﺍﻫﺪ ﺷﺪ. ﺩﺭ ﺻﻮﺭﺕ ﻧﻴﺎﺯ ﺑﻪ ﺍﻃﻼﻋﺎﺕ ﺑﻴﺸﺘﺮ ﻣﻲﺗﻮﺍﻧﻴﺪ ﺑﺎ ganji@ce.sharif.eduﻳﺎ falaki@ce.sharif.eduﺗﻤﺎﺱ ﺑﮕﻴﺮﻳﺪ.
© Copyright 2025 Paperzz