31
Deadlock • What is deadlock? – A group of processes permanently stuck waiting for each other • Deadlock Example Process P Process Q (1) Get A (5) Get B (2) Get B (6) Get A (3) Release B (7) Release A (4) Release A (8) Release B • Possible execution paths – 5, 6, 7, 8, … – No problem – 5, 6, 1, … – P blocks, but will finish – 5, 1, … – Deadlock inevitable – 1, 5, … – Deadlock inevitable – 1, 2, 5, … – Q blocks, but will finish – 1, 2, 3, 4, … – No problem

Chap06

Embed Size (px)

DESCRIPTION

OS SLIDES

Citation preview

  • DeadlockWhat is deadlock?A group of processes permanently stuck waiting for each otherDeadlock ExampleProcess PProcess Q(1) Get A(5) Get B(2) Get B(6) Get A(3) Release B(7) Release A(4) Release A(8) Release BPossible execution paths5, 6, 7, 8, No problem5, 6, 1, P blocks, but will finish5, 1, Deadlock inevitable1, 5, Deadlock inevitable1, 2, 5, Q blocks, but will finish1, 2, 3, 4, No problem

  • DeadlockReusable ResourcesCan only be used by one process at a time. After use, can be reassigned to another process (printer, memory, files, etc.)In previous example, if A = file, and B = tape drive, deadlock can occur Can occur with memory allocation if we dont do swapping200K available, both processes want 80K, then make second request for 70KConsumable ResourcesCan be created or destroyed (signals, messages)No fixed limit on # of resourcesCan deadlock if both waiting for a message from the other

  • Conditions for DeadlockAll must hold for deadlock to occurMutual ExclusionOnly one process can hold the resource at any given timeHold and WaitA process may hold resources while requesting othersNo PreemptionNo resource may be forcibly removed from a processCircular Wait (event)A closed chain of process exists, each waiting for a resource held by the next process in the chainFigure 6.5, page 273:

  • Deadlock PreventionCan prevent deadlock by forbidding one of the conditionsForbid Mutual ExclusionGenerally not reasonableMultiple read-only access often okForbid Hold and WaitAsk for all resources at one timeMay block a process for a long time when it can be making progressWait for resources it doesnt yet needMay tie up unneeded resourcesMay hold resources that are not needed immediatelyMay not know what to requestEspecially true for interactive processesMay request resources because they are needed in some instances, but not always

  • Deadlock PreventionForbid No PreemptionTake resources away from waiting processesOnly feasible if state can be savedExamples: CPU, MemoryNot usable for Files, Printer, etc.Forbid Circular WaitDefine a linear order on itemsIf it needs resources 3, 15, 6, 9, and 4 then must request in the order 3, 4, 6, 9, 15Cannot have circular wait because a process cannot have 9 and request 5May not be easy to define the order (files)May force an awkward order of requesting resourcesMay have to request resource 3 in order to request 4, even when 4 is needed now but 3 is not needed until much later

  • Deadlock AvoidanceAllow general requests, but make choices to avoid deadlockAssume we know the maximum requests (claims) for each processProcess must state it needs a max of 5 A objects, 3 B objects, 2 C objects.Do not need to use its max claimsOk to set max=5 and only use 3Can make requests at any time and in any orderProcess Initiation DenialTrack current allocationsAssume all processes may make maximum requests at the same timeOnly start process if it cant result in deadlock regardless of allocations

  • Resource Allocation DenialAlso called bankers algorithmsafe state We can finish all processes by some scheduling sequenceExample: Finish P1, P4, P2, P5, P3Reject a request if it exceeds the processes declared maximum claimsGrant a request if the new state would be safeDetermining if a state is safeFind any process Pi for which we can meet its maximum requestsDon't forget already allocated resourcesMark Pi as done, add its resources to available resource poolState is safe if we can mark all processes as doneBlock a process if the resources are not currently available or the new state is not safe

  • Avoidance ExampleFigure 6.6, page 277Claim Matrix:ABCP1322P2613P3314P4422Allocation Matrix:ABCP1100P2511P3211P4002Total Resources:936Available:112P1 wants 1 A and 1 C, is it ok?P4 wants 1 B, is it ok?Still requires the process to declare its max request at start

  • Deadlock DetectionAvoidance methods tend to limit access to resourcesInstead, grant arbitrary requests and note when deadlock happensCan vary how often we checkEarly detection vs. overhead of checksAlgorithm (Page 280)Preparation:Create table of process requests, current allocationsNote available resourcesMark processes with no resourcesMark any process whose requests can be met (requests available resources)Include resources from that process as available (this process can finish)If multiple processes available, pick anyIf any processes cannot be marked, they are part of a deadlock

  • Detection ExampleFigure 6.9, page 281Requests:A B C D EP10 1 0 0 1P20 0 1 0 1P30 0 0 0 1P41 0 1 0 1Allocation:A B C D EP11 0 1 1 0P21 1 0 0 0P30 0 0 1 0P40 0 0 0 0Available0 0 0 0 1Mark P4 (nothing allocated)Mark P3, new available: 0 0 0 1 1Cannot mark P1 or P2, so we are deadlocked

  • Detection Example 2Requests:A B CP10 0 0P22 0 2P30 0 0P41 0 0P50 0 2Allocation:A B CP10 1 0P22 0 0P33 0 3P42 1 1P50 0 2Available0 0 0Is this system deadlocked?

  • Deadlock RecoverySeveral possible approachesAbort all deadlocked processesSimple but commonBack up processes to a previously saved checkpoint, then restartAssumes we have checkpoints and a rollback mechanismRuns risk of repeating deadlockAssumes that the deadlock has enough timing dependencies it wont happenSelectively abort processes until deadlock brokenPreempt resources until deadlock brokenMust roll back process to checkpoint prior to acquiring key resource

  • Mixed StrategyMay group resources into classes, have a different deadlock strategy for each classSwap SpacePrevent Deadlocks by requiring all space to be allocated at onceAvoidance also possibleTapes/FilesAvoidance can be effective herePrevention by ordering resources also possibleMain MemoryPreemption a good approachInternal Resources (channels, etc.)Prevention by ordering resourcesCan use linear ordering between classes

  • Dining PhilosophersFive philosophers who alternately think and eatShares a fork with each neighborAssume each philosopher picks up left fork, then right fork, then eatsDeadlock if all enter at onceFigure 6.10, page 283

  • Solving Dining Phil.Buy more ForksEquivalent to increasing resourcesPut fork down if 2nd fork busyCan produce livelock if philosophers stay synchronizedRoom AttendantOnly let 4 of the philosophers into the room at onceMay have 4 philosophers in room, but only 1 can eatLeft-Handed PhilosophersGrab forks in the other order (right fork, then left fork)Any mix will avoid deadlock (effective linear ordering on forks)

  • Linux Fork()fork()returns 0 to childreturn child_pid to parentParent is responsible to look after childrenFailure to do so can create zombiespid = wait( &status ) to explicitly wait for the child to terminatesignal(SIGCHLD, SIG_IGN) to ignore child-termination signalsMay also set SIGCHLD to call a specified subroutine when a child dies

  • Fork() Example#include #include void main(){ int cnt = 15; int pid; char buff[100];/* create a new process */ if ((pid = fork()) == -1) { perror("Fork failed, call system manager!"); exit(1); }if (pid) {/* we are the parent, tell system to ignore child termination messages */ signal(SIGCHLD, SIG_IGN);

    while (cnt--) {/* lets look for the child */ printf("The child has a pid of %d, my pid is %d\n", pid, getpid()); printf("----------------------\n"); sprintf(buff, "%s %d", "ps -aux | grep ", pid); system(buff); /* execute the command */ printf("----------------------\n");

    sleep(1);/* let some time pass */

    } exit(0); } else {/* here is the child code */ int cnt = 8; while (cnt) { printf("[child] I'm alive...cnt = %d\n", cnt--); sleep(1); } printf("[child] Preparing to exit with status 7\n"); exit(7); /* quit */ }} /* end of program */

  • Linux SemaphoresKey - 4 byte value for the nameCreate/access semaphoresid = semget( KEY, Count,IPC_CREAT | PERM )id = semget( KEY, 0, 0 )Perm = file permission bits (0666)to create only (fails if exists) useIPC_CREAT|IPC_EXCL|PERMCount = # of semaphores to createWorking with semaphore valuei = semctl( id, num, SETVAL, v )val= semctl( id, num, GETVAL, 0 )Deleting semaphoresi = semctl( id, 0, IPC_RMID, 0 )ipcs Program to list semaphores/etc.ipcrm sem id Program to delete a semaphore

  • Semaphore OperationsSemaphore Structurestruct sembuf {short sem_num;(semaphore ID)short sem_op;(change amount)short sem_flg;(wait/dont wait)} op;sem_flg can be 0 or IPC_NOWAITWait():op.sem_op = -1(decrement)res = semop( id, &op, 1 )Signal():op.sem_op = 1(increment)res = semop( id, &op, 1 )Can do multiple signal/wait opsres = semop( id, oparray, opcount )

  • Create Example#include #include #include #include

    #define PERM 0666#define KEY 0x12348765

    main(){int id, result;

    id = semget(KEY,1,IPC_CREAT|IPC_EXCL|PERM); printf("status of create = %d\n", id); if(id == -1){perror("Bad Semaphore Create"); exit(0);}

    /* set the value to one */ result = semctl(id,0,SETVAL,1); if(result == -1){ perror("Bad SetVal call");exit(0);} printf("Semaphore created with an id of %d\n",id);}

  • Wait Example#include #include #include #include #define KEY 0x12348765

    main(){int id, result; struct sembuf op[1];

    id = semget(KEY,0,0); printf("status of get key = %d\n",id); if(id == -1) {perror("Bad Semaphore ID fetch"); exit();}

    /* initialize the op data structure */ op[0].sem_num = 0; /* the first one */ op[0].sem_op = -1; /* wait until we can add 1 */ op[0].sem_flg = 0; /* no options */

    /* now try to get past the semaphore */ printf("Ready to test semaphore\n"); result = semop(id,op,1); if(result == -1){ perror("Bad Wait"); exit();} printf("Got past semaphore\n"); }

  • Shared MemoryCreate/Access Shared Memoryid = shmget( KEY, Size,IPC_CREAT | PERM )id = shmget( KEY, 0, 0 )Deleting Shared Memoryi = shmctl( id, IPC_RMID, 0 )Or use ipcrmAccessing Shared Memorymemaddr = shmat( id, 0, 0 )memaddr = shmat( id, addr, 0 )Addr should be multiple of SHMLBAmemaddr = shmat( id, 0, SHM_READONLY )System will decide address to place the memory atshmdt( memaddr )Detach from shared memory

  • Creating Shared Memory#include #include #include #include

    #define SIZE512/* shared mem buffer size */#define PERM0666/* all can read write */#define KEY'JHL '/* our key */

    main(){int id;

    id = shmget(KEY,SIZE, IPC_CREAT|IPC_EXCL|PERM );printf("status of create = %d\n",id);if(id == -1) perror("Bad Create"); }

  • Writing Shared Memory#include #include #include #include #include #include

    #define SIZE 512/* shared memory buffer size */#define KEY 'JHL ' /* our key */main(){key_t key;int memoryid;char * location;/* the shmget will fail if the segment has not been created */memoryid = shmget(KEY,SIZE,0);printf("status of key retrieval = %d\n",memoryid);if(memoryid == -1) {perror("Bad Key");exit();}/* we got the key now attach to the segmentwe will allow the system to chose the location (second parm)and we will want read/write access (third parm)*/location = (char *)shmat(memoryid,0,0); /* we want read write access */printf("status of attach = %d\n",location);if (location == (char*)-1) {perror("Bad Attach");exit();}/* memory has been attached to,now copy string into the location */printf("Enter string -> ");gets(location);printf("location contains: %s\n",location);}

  • Unix PipesCreates a one-way connection between processesCreated using pipe() callReturns a pair of file descriptorspend[0] = read endpend[1] = write endUse dup() call to convert to stdin/stdoutExample:int pend[2]pipe( pend )fork()parentchildclose(pend[1])close(pend[0]) write(pend[1],)read(pend[0],)close(pend[0])close(pend[1])

  • Unix Pipe Example/* A simple "pipe" demo --- Author: Jerry LeVan */#include #define DATA "Sample Data Sent Through A Pipe"main(){int sockets[2], child;/* create a pipe; doing it here assures that it will be open in the child */if(pipe(sockets)
  • Unix MessagesSend information (type + data) between processesMessage Structurelong typechar text[]Functions:id = msgget( KEY, IPC_CREAT|)id = msgget( KEY, 0 )msgctl( id, IPC_RMID )msgsnd( id, buf, text_size, flags )msgrcv( id, buf, max_size, flags )Useful FlagsIPC_NOWAITMSG_NOERROR (truncate long messages to max_size)

  • Other Unix SynchronizationSemaphores (discussed earlier)Shared Memory (discussed earlier)SignalsUsed to inform processes of asynchronous eventsSet up handler usingvoid handler( int sig_num ) { }sig_num is signal number so one handler can respond to several signalssignal( SIGxxx, handler)One-time event Must reset within handler if you want to keep handling this signal (Linux)special value for handlerSIG_IGN Ignore signalSIG_DFL Default actionPartial list (Table 6.2, page 288)SIGHUP, SIGQUIT, SIGKILL, SIGALARM, SIGUSR1, SIGCLDSend signal using kill command/call

  • SolarisBoth Kernel and User-level callsMutual Exclusion Lockmutex_enter() Waiting processes can busy-wait or be suspendedmutex_exit() Release lockmutex_tryenter() Get lock if not heldCondition VariablesUsed in conjunction with a mutex lockcv_wait() The process will release the associated mutex before waiting, reacquire it before continuingcv_signal() Wake up one thread waiting on a given conditioncv_broadcast() Wake up all threadsMutex + Condition Variables can be used to implement monitors

  • SolarisSemaphoressema_p() Traditional Wait()sema_v() Traditional Signal()sema_tryp()Reader/Writer Lockrw_enter() Get lock as a reader or writerrw_exit() Release a lockrw_tryenter() Try to get a lock, but dont wait if it isnt availablerw_downgrade() Convert a write lock to a read lockrw_tryupgrade() Try to convert a read lock into a write lock

  • Windows 2000Synchronization Objects (table 6.3, page 292)Process Signals on terminationThread Signals on terminationFile Signal on I/O operation completeConsole Input Signal on data readyFile Change Notification Signal when a file system change matching a filter happensMutex Provides mutual exclusionSemaphore Counting semaphoreEvent Announcement a system event has happenedWaitable Timer Counter that records the passage of time