Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Linux and C Here is the code for the old lab they mentioned: #include <stdio.h>

ID: 3813024 • Letter: L

Question

Linux and C

Here is the code for the old lab they mentioned:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main()
{
int shmid, status;
int *a, *b;
int i;

shmid = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
int *temp=(int *)shmat(shmid,0,0);
*temp=19530;

int start=1;
// for(i=0;i<5;i++)
// {
if (fork() == 0) {

b = (int *) shmat(shmid, 0, 0);
if(start==1)
{b[0]=19530;
start=0;
}
for( i=0; i< 5; i++) {
if(i==0)
b[0]=19530;
sleep(1);
b[0]-=5;
printf(" Child reads: %d ",b[0]);
}


shmdt(b);

}
else {

a = (int *) shmat(shmid, 0, 0);

  
for( i=0; i< 5; i++) {
sleep(1);
a[0] = a[0]/5;
printf("Parent writes: %d, ",a[0]);
}
wait(&status);

shmdt(a);

shmctl(shmid, IPC_RMID, 0);
}
}

Read the man page of the following functions pthread-create, pthread-join, pthread mutex-init, pthread mutex-lock, pthread-mu pthread.mutex-unlock, pthread-mutex-destroy. 3 par- tially completed programs have been provided for you to help teach you threads. For this lab you are to create an output similar to that of the previous lab (x-5, x/5, etc.). This time you are using threads though. Since threads share global variables, you may make x a global variable, thus you don't have to use a file or shared memory to modify it. There are two ways to do this lab, and you may choose either, but you must use multithreading: 1. You can create your threads inside the loop, and use pthread-join0 to control the flow. Thus 10 total threads will be created. 2. You can make each thread have its own loop, and use mutexes to control the flow. Thus 2 total threads are created. Following is a sample of the output that will be printed to the screen/terminal.

Explanation / Answer

public category TTTConsoleNonOO2P cipher = 2;

// Name-constants to represent the varied states of the sport
public static final int enjoying = 0;
public static final int DRAW = 1;
public static final int CROSS_WON = 2;
public static final int NOUGHT_WON = 3;

// the sport board and also the game standing
public static final int ROWS = three, COLS = 3; // range of rows and columns
public static int[][] board = new int[ROWS][COLS]; // game board in 2nd array
// containing (EMPTY, CROSS, NOUGHT)
public static int currentState; // this state of the sport
// (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
public static int currentPlayer; // this player (CROSS or NOUGHT)
public static int currntRow, currentCol; // current seed's row and column

public static Scanner in = new Scanner(System.in); // the input Scanner

/** The entry main methodology (the program starts here) */
public static void main(String[] args) {
// Initialize the game-board and current standing
initGame();
// Play the sport once
do else if (isDraw())
// Otherwise, no amendment to currentState (still PLAYING).
}

/** come back true if it's a draw (no additional empty cell) */
// TODO: Shall declare draw if no player will "possibly" win
public static Boolean isDraw() mountain pass = 0; gap &lt; COLS; ++col) {
if (board[row][col] == EMPTY) {
come back false; // AN empty cell found, not draw, exit
}
}
}
come back true; // no empty cell, it is a draw
}

/** come back true if the player with "theSeed" has won once inserting at
(currentRow, currentCol) */
public static Boolean hasWon(int theSeed, int currentRow, int currentCol) {
come back (board[currentRow][0] == theSeed // 3-in-the-row
&amp;&amp; board[currentRow][1] == theSeed
&amp;&amp; board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed // 3-in-the-column
&amp;&amp; board[1][currentCol] == theSeed
&amp;&amp; board[2][currentCol] == theSeed
|| currentRow == currentCol // 3-in-the-diagonal
&amp;&amp; board[0][0] == theSeed
&amp;&amp; board[1][1] == theSeed
&amp;&amp; board[2][2] == theSeed
|| currentRow + currentCol == two // 3-in-the-opposite-diagonal
&amp;&amp; board[0][2] == theSeed
&amp;&amp; board[1][1] == theSeed
&amp;&amp; board[2][0] == theSeed);
}

/** Print the sport board */
public static void printBoard() mountain pass = 0; gap &lt; COLS; ++col) every of the cells
if (col != COLS - 1)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote