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);
}
}
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 < 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
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed // 3-in-the-column
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol // 3-in-the-diagonal
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == two // 3-in-the-opposite-diagonal
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}
/** Print the sport board */
public static void printBoard() mountain pass = 0; gap < COLS; ++col) every of the cells
if (col != COLS - 1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.