Snippets Collections
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main() {
    int fd[2]; // Array to hold the two ends of the pipe: fd[0] for reading, fd[1] for writing
    pid_t pid;
    char writeMessage[] = "Hello from parent process!";
    char readMessage[100];

    // Create the pipe
    if (pipe(fd) == -1) {
        perror("pipe failed");
        return 1;
    }

    // Fork a child process
    pid = fork();

    if (pid < 0) {
        perror("fork failed");
        return 1;
    } else if (pid > 0) {
        // Parent process
        close(fd[0]); // Close the reading end of the pipe in the parent

        // Write a message to the pipe
        write(fd[1], writeMessage, strlen(writeMessage) + 1);
        close(fd[1]); // Close the writing end of the pipe after writing

        // Wait for child process to finish
        wait(NULL);
    } else {
        // Child process
        close(fd[1]); // Close the writing end of the pipe in the child

        // Read the message from the pipe
        read(fd[0], readMessage, sizeof(readMessage));
        printf("Child process received: %s\n", readMessage);
        close(fd[0]); // Close the reading end of the pipe after reading
    }

return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#define FIFO_NAME "myfifo"

int main() {
    int fd;
    char *message = "Hello from writer process!";
    
    // Create the FIFO if it does not exist
    if (mkfifo(FIFO_NAME, 0666) == -1) {
        perror("mkfifo");
        exit(EXIT_FAILURE);
    }

    // Open the FIFO for writing
    fd = open(FIFO_NAME, O_WRONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    // Write the message to the FIFO
    if (write(fd, message, strlen(message) + 1) == -1) {
        perror("write");
        close(fd);
        exit(EXIT_FAILURE);
    }

    printf("Writer: Wrote message to FIFO.\n");
    
    // Close the FIFO
    close(fd);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_FRAMES 100
int isPageInFrame(int frames[], int n, int page) {
    for (int i = 0; i < n; i++) {
        if (frames[i] == page) {
            return 1;
        }
    }
    return 0;
}
void printFrames(int frames[], int n) {
    for (int i = 0; i < n; i++) {
        if (frames[i] == -1) {
            printf("-");
        } else {
            printf("%d", frames[i]);
        }
        if (i < n - 1) {
            printf(" ");
        }
    }
    printf("\n");
}
int main() {
    int n, numFrames;
    int pageFaults = 0;
    printf("Enter the number of frames: ");
    scanf("%d", &numFrames);
    int frames[MAX_FRAMES];
    for (int i = 0; i < numFrames; i++) {
        frames[i] = -1;
    }
    printf("Enter the number of page requests: ");
    scanf("%d", &n);
    int pageRequests[n];
    printf("Enter the page requests: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &pageRequests[i]);
    }
    int currentFrame = 0;
    for (int i = 0; i < n; i++) {
        int page = pageRequests[i];
        if (!isPageInFrame(frames, numFrames, page)) {
            pageFaults++;
            frames[currentFrame] = page;
            currentFrame = (currentFrame + 1) % numFrames;
        }
        printFrames(frames, numFrames);
    }
    printf("Total page faults: %d\n", pageFaults);
    return 0;
}
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main() {
    // Generate unique key
    key_t key = ftok("shmfile", 65);
    if (key == -1) {
        perror("ftok");
        return 1;
    }

    // Get the shared memory segment
    int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
    if (shmid == -1) {
        perror("shmget");
        return 1;
    }

    // Attach to the shared memory
    char *str = (char*)shmat(shmid, (void*)0, 0);
    if (str == (char*)(-1)) {
        perror("shmat");
        return 1;
    }

    // Read data from shared memory
    printf("Data read from memory: %s\n", str);

    // Detach from shared memory
    if (shmdt(str) == -1) {
        perror("shmdt");
        return 1;
    }

    // Destroy the shared memory
    if (shmctl(shmid, IPC_RMID, NULL) == -1) {
        perror("shmctl");
        return 1;
    }

    return 0;
}
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

int main() {
    // Generate unique key
    key_t key = ftok("shmfile", 65);
    if (key == -1) {
        perror("ftok");
        return 1;
    }

    // Create shared memory segment
    int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
    if (shmid == -1) {
        perror("shmget");
        return 1;
    }

    // Attach to shared memory
    char *str = (char*)shmat(shmid, (void*)0, 0);
    if (str == (char*)(-1)) {
        perror("shmat");
        return 1;
    }

    // Write data to shared memory
    printf("Write Data : ");
    fgets(str, 1024, stdin);
    // Remove the newline character added by fgets
    str[strcspn(str, "\n")] = 0;

    printf("Data written in memory: %s\n", str);

    // Detach from shared memory
    if (shmdt(str) == -1) {
        perror("shmdt");
        return 1;
    }

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int file;
    off_t offset;
    char buffer[20];

    // Open file
    file = open("file.txt", O_RDONLY);
    if (file == -1) {
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }

    // Move file offset to 10 bytes from the beginning
    offset = lseek(file, 10, SEEK_SET);
    if (offset == -1) {
        perror("Error seeking in file");
        close(file);
        exit(EXIT_FAILURE);
    }

    // Read from the new offset
    if (read(file, buffer, 20) == -1) {
        perror("Error reading from file");
        close(file);
        exit(EXIT_FAILURE);
    }

    // Print the read content
    printf("Read content: %.*s\n", 20, buffer);

    // Close file
    close(file);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main() {
    DIR *dir;
    struct dirent *entry;

    // Open directory
    dir = opendir(".");
    if (dir == NULL) {
        perror("Error opening directory");
        exit(EXIT_FAILURE);
    }

    // Read directory entries
    while ((entry = readdir(dir)) != NULL) {
        printf("%s\n", entry->d_name);
    }

    // Close directory
    closedir(dir);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main() {
    struct stat fileStat;

    // Get file information
    if (stat("file.txt", &fileStat) == -1) {
        perror("Error getting file information");
        exit(EXIT_FAILURE);
    }

    // Print file information
    printf("File size: %ld bytes\n", fileStat.st_size);
    printf("File permissions: %o\n", fileStat.st_mode & 0777);
    printf("Last access time: %ld\n", fileStat.st_atime);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#define BUFFER_SIZE 1024

int main() {
    int sourceFile, destFile;
    ssize_t bytesRead, bytesWritten;
    char buffer[BUFFER_SIZE];

    // Open source file
    sourceFile = open("source.txt", O_RDONLY);
    if (sourceFile == -1) {
        perror("Error opening source file");
        exit(EXIT_FAILURE);
    }

    // Open destination file
    destFile = open("dest.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (destFile == -1) {
        perror("Error opening destination file");
        close(sourceFile);
        exit(EXIT_FAILURE);
    }

    // Read from source and write to destination
    while ((bytesRead = read(sourceFile, buffer, BUFFER_SIZE)) > 0) {
        bytesWritten = write(destFile, buffer, bytesRead);
        if (bytesWritten != bytesRead) {
            perror("Error writing to destination file");
            close(sourceFile);
            close(destFile);
            exit(EXIT_FAILURE);
        }
    }

    if (bytesRead == -1) {
        perror("Error reading from source file");
    }

    // Close files
    close(sourceFile);
    close(destFile);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int file;
    int flags;

    // Open file
    file = open("file.txt", O_RDWR);
    if (file == -1) {
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }

    // Get file descriptor flags
    flags = fcntl(file, F_GETFL);
    if (flags == -1) {
        perror("Error getting file flags");
        close(file);
        exit(EXIT_FAILURE);
    }

    // Set file descriptor flags to append mode
    flags |= O_APPEND;
    if (fcntl(file, F_SETFL, flags) == -1) {
        perror("Error setting file flags");
        close(file);
        exit(EXIT_FAILURE);
    }

    // Write to file
    if (write(file, "Appending this line\n", 20) == -1) {
        perror("Error writing to file");
    }

    // Close file
    close(file);

    return 0;
}
#include<stdio.h>
 int main()
{
    int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
    float avg_wt,avg_tat;
    printf("Enter number of process:");
    scanf("%d",&n); 
    printf("nEnter Burst Time:n");
    for(i=0;i<n;i++)
    {
        printf("p%d:",i+1);
        scanf("%d",&bt[i]);
        p[i]=i+1;         
    }
    for(i=0;i<n;i++)
    {
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(bt[j]<bt[pos])
                pos=j;
        }
        temp=bt[i];
        bt[i]=bt[pos];
        bt[pos]=temp;
  
        temp=p[i];
        p[i]=p[pos];
        p[pos]=temp;
    }
    wt[0]=0;               
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];
        total+=wt[i];
    }
    avg_wt=(float)total/n;      
    total=0;
    printf("nProcesst    Burst Time    tWaiting TimetTurnaround Time");
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];   
        total+=tat[i];
        printf("np%dtt  %dtt    %dttt%d",p[i],bt[i],wt[i],tat[i]);
    }
    avg_tat=(float)total/n;    
    printf("nnAverage Waiting Time=%f",avg_wt);
    printf("nAverage Turnaround Time=%fn",avg_tat);
}








#include<stdio.h>
 int main()
{
int process,resource,instance,j,i,k=0,count1=0,count2=0;
int avail[10] , max[10][10], allot[10][10],need[10][10],completed[10];
printf("\n\t\t Enter No. of Process: "); 
scanf("%d",&process); 
printf("\n\t\tEnter No. of Resources: "); 
scanf("%d",&resource); 
for(i=0;i<process;i++)
completed[i]=0;
printf("\n\t Enter No. of Available Instances: ");
 for(i=0;i<resource;i++)
{
scanf("%d",&instance); 
avail[i]=instance;
}
printf("\n\tEnter Maximum No. of instances of resources that a Process need:\n");
for(i=0;i<process;i++)
{
printf("\n\t For P[%d]",i);
 for(j=0;j<resource;j++)
{
printf("\t"); 
scanf("%d",&instance); 
max[i][j]=instance;
}
}
printf("\n\t Enter no. of instances already allocated to process of a resource:\n");
for(i=0;i<process;i++)
{
printf("\n\t For P[%d]\t",i); 
for(j=0;j<resource;j++)
{
scanf("%d",&instance);
 allot[i][j]=instance;
need[i][j]=max[i][j]-allot[i][j];//calculating  Need  of  each process
		}	
}
printf("\n\n \t Safe Sequence is:- \t"); 
while(count1!=process)
{
count2=count1; 
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(need[i][j]<=avail[j])
{
k++;
}
}
if(k==resource  &&  completed[i]==0  )
{
printf("P[%d]\t",i); 
completed[i]=1; 
for(j=0;j<resource;j++)
{
avail[j]=avail[j]+allot[i][j];
}
count1++;
}
			k=0;	
}
if(count1==count2)
{
printf("\t\t Stop ..After this.....Deadlock \n");
 break;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
int n, k;
int t[(int)1e5 + 1];

int frogJump(vector<int>& a, int n, int k) {
    if (n == 1) return 0; // Starting point, no cost

    if (t[n] != -1) return t[n];

    int minCost = INT_MAX;
    for (int i = 1; i <= k; ++i) {
        if (n - i >= 1) {
            minCost = min(minCost, frogJump(a, n - i, k) + abs(a[n - 1] - a[n - i - 1]));
        }
    }
    t[n] = minCost % mod;
    return t[n];
}

int main() {
    memset(t, -1, sizeof(t));

    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    int result = frogJump(a, n, k);
    cout << result << endl;
    return 0;
}
#include <stdio.h>

#include <stdlib.h>

typedef struct {

    int base;

    int limit;

} Segment;

void simulateSegmentation(int segments[], int numSegments) {

    int memory[100];  // Simulate memory with an array (100 units for example)

    int currentBase = 0;

    // Allocate segments in memory

    for (int i = 0; i < numSegments; i++) {

        if (currentBase + segments[i] <= 100) {

            printf("Allocating segment %d with size %d at base %d\n", i, segments[i], currentBase);

            for (int j = currentBase; j < currentBase + segments[i]; j++) {

                memory[j] = 1;  // Mark memory as occupied

            }

            currentBase += segments[i];

        } else {

            printf("Not enough memory for segment %d with size %d\n", i, segments[i]);

        }

    }

}

int main() {

    int numSegments;

    printf("Enter number of segments: ");

    scanf("%d", &numSegments);

    int segments[numSegments];

    for (int i = 0; i < numSegments; i++) {

        printf("Enter size of segment %d: ", i);

        scanf("%d", &segments[i]);

    }

    simulateSegmentation(segments, numSegments);

    return 0;

}
#include <stdio.h>

#include <stdlib.h>

#define PAGE_SIZE 4  // Define page size (4KB for example)

#define MEMORY_SIZE 32  // Define memory size (32KB for example)

void simulatePaging(int processSize) {

    int numPages = (processSize + PAGE_SIZE - 1) / PAGE_SIZE;  // Calculate the number of pages required

    printf("Process of size %dKB requires %d pages.\n", processSize, numPages);

    int memory[MEMORY_SIZE / PAGE_SIZE];  // Simulate memory with an array

    // Initialize memory (0 means free, 1 means occupied)

    for (int i = 0; i < MEMORY_SIZE / PAGE_SIZE; i++) {

        memory[i] = 0;

    }

    // Allocate pages to the process

    for (int i = 0; i < numPages; i++) {

        for (int j = 0; j < MEMORY_SIZE / PAGE_SIZE; j++) {

            if (memory[j] == 0) {

                memory[j] = 1;

                printf("Allocating page %d to frame %d\n", i, j);

                break;

            }

        }

    }

}

int main() {

    int processSize;

    printf("Enter process size (in KB): ");

    scanf("%d", &processSize);

    simulatePaging(processSize);

    return 0;

}
package org.example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Avaliacao {

    private Cliente cliente;
    private Funcionario funcionario;

    private int idade;
    private double peso;
    private String sexo;
    private String data;
    private double altura;
    private double massaMuscular;
    private double imc;
    private double percentualGordura;

    public Avaliacao(Cliente cliente, Funcionario funcionario, int idade, double peso, String sexo, String data, double altura) {
        this.cliente = cliente;
        this.funcionario = funcionario;
        this.idade = idade;
        this.peso = peso;
        this.sexo = sexo;
        this.data = data;
        this.altura = altura;

    }


    //getters e setters
    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Funcionario getFuncionario() {
        return funcionario;
    }

    public void setFuncionario(Funcionario funcionario) {
        this.funcionario = funcionario;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

    public double getPeso() {
        return peso;
    }

    public void setPeso(double peso) {
        this.peso = peso;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public double getAltura() {
        return altura;
    }

    public void setAltura(double altura) {
        this.altura = altura;
    }

    public double getMassaMuscular() {
        return massaMuscular;
    }

    public void setMassaMuscular(double massaMuscular) {
        this.massaMuscular = massaMuscular;
    }

    public double getImc() {
        return imc;
    }

    public void setImc(double imc) {
        this.imc = imc;
    }

    public double getPercentualGordura() {
        return percentualGordura;
    }

    public void setPercentualGordura(double percentualGordura) {
        this.percentualGordura = percentualGordura;
    }


    //metodos


    // Método para converter a data de String para Date
    public Date getDataAsDate() throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        return formatter.parse(this.data);
    }

    //metodo calcular  IMC

    private double calcularIMC() {

        return peso / (altura * altura);
    }


    //metodo calcular massa muscular (forma simplificada)

    private double calcularMassaMuscular() {
        return peso * (1 - percentualGordura / 100);
    }

    //metodo calcular % de gordura (formula simplificada)

    private double calcularPercentualGordura() {
        //  usando IMC, idade e sexo (simplificado)

        double fatorSexo = sexo.equalsIgnoreCase("masculino") ? 10.8 : 0;

        return (1.2 * imc) + (0.23 * idade) - fatorSexo - 5.4;

    }


    public void atualizarCalculos() {
        this.imc = calcularIMC();
        this.massaMuscular = calcularMassaMuscular();
        this.percentualGordura = calcularPercentualGordura();
    }


    @Override
    public String toString() {
        return "=================" +
                "\n cliente=" + cliente +
                "\n funcionario=" + funcionario +
                "\n idade=" + idade +
                "\n peso=" + peso +
                "\n sexo='" + sexo + '\'' +
                "\n data='" + data + '\'' +
                "\n altura=" + altura +
                "\n massaMuscular=" + massaMuscular +
                "\n imc=" + imc +
                "\n percentualGordura=" + percentualGordura;
    }
}
#include <stdio.h>
#include <stdbool.h>
#define P 5
#define R 3
int available[R], max[P][R], allocation[P][R], need[P][R];
void calculate_need() {
    for (int i = 0; i < P; i++)
        for (int j = 0; j < R; j++)
            need[i][j] = max[i][j] - allocation[i][j];
}
bool is_safe(int safe_seq[]) {
    int work[R];
    bool finish[P] = {0};
    for (int i = 0; i < R; i++) work[i] = available[i];
    int count = 0;
    while (count < P) {
        bool found = false;
        for (int p = 0; p < P; p++) {
            if (!finish[p]) {
                int j;
                for (j = 0; j < R; j++)
                    if (need[p][j] > work[j]) break;
                if (j == R) {
                    for (int k = 0; k < R; k++) work[k] += allocation[p][k];
                    finish[p] = true;
                    safe_seq[count++] = p;
                    found = true;
                }
            }
        }
        if (!found) return false;
    }
    return true;
}
void input_data() {
    printf("Enter available resources: ");
    for (int i = 0; i < R; i++) scanf("%d", &available[i]);
    printf("Enter maximum demand:\n");
    for (int i = 0; i < P; i++) for (int j = 0; j < R; j++) scanf("%d", &max[i][j]);
    printf("Enter allocated resources:\n");
    for (int i = 0; i < P; i++) for (int j = 0; j < R; j++) scanf("%d", &allocation[i][j]);
}
int main() {
    input_data();
    calculate_need();
    int safe_seq[P];
    if (is_safe(safe_seq)) {
        printf("System is in a safe state.\nSafe sequence is: ");
        for (int i = 0; i < P; i++) printf("%d ", safe_seq[i]);
        printf("\n");
    } else {
        printf("System is not in a safe state.\n");
    }
    return 0;
}
#include <stdio.h>
int main() {
    int n;
    printf("Enter number of processes: ");
    scanf("%d", &n);
    int priority[n], burst[n], wait[n], turn[n];
    float wt=0,tat=0;
    printf("Enter priority and burst times: ");
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &priority[i], &burst[i]);
    }
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            if (priority[j] > priority[j+1]) {
                int temp = priority[j];
                priority[j] = priority[j+1];
                priority[j+1] = temp;
                temp = burst[j];
                burst[j] = burst[j+1];
                burst[j+1] = temp;
            }
        }
    }
    
    wait[0] = 0;
    for (int i = 1; i < n; i++) {
        wait[i] = wait[i - 1] + burst[i - 1];
        wt+=wait[i];
    }

    for (int i = 0; i < n; i++) {
        turn[i] = wait[i] + burst[i];
        tat+=turn[i];
        printf("Process %d: Priority %d, Burst Time %d, Waiting Time %d, Turnaround Time %d\n", i + 1, priority[i], burst[i], wait[i], turn[i]);
    }
    printf("\nthe avg wt: %.2f\n",wt/n);
    printf("the avg tat: %.2f\n",tat/n);

    return 0;
}
#include <stdio.h>
int main() {
    int n, tq, time = 0, totalWait = 0, totalTurn = 0;
    printf("Enter the number of processes and time quantum: ");
    scanf("%d %d", &n, &tq);
    int bt[n], remain[n], wt[n], tat[n];
    printf("Enter the burst times: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &bt[i]);
        remain[i] = bt[i];
        wt[i] = 0;
    }
    while (1) {
        int done = 1;
        for (int i = 0; i < n; i++) {
            if (remain[i] > 0) {
                done = 0;
                if (remain[i] > tq) {
                    time += tq;
                    remain[i] -= tq;
                } else {
                    time += remain[i];
                    wt[i] = time - bt[i];
                    remain[i] = 0;
                }
            }
        }
        if (done) break;
    }
    for (int i = 0; i < n; i++) {
        tat[i] = wt[i] + bt[i];
        totalWait += wt[i];
        totalTurn += tat[i];
        printf("P%d %d %d %d\n", i + 1, bt[i], wt[i], tat[i]);
    }
    printf("Average Waiting Time: %.2f\n", (float)totalWait / n);
    printf("Average Turnaround Time: %.2f\n", (float)totalTurn / n);
    return 0;
}
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
extern int errno;
int main( )
{     
    // if file does not have in directory 
    // then file foo.txt is created.
    int fd = open("foo.txt", O_RDONLY | O_CREAT); 
     
    printf("fd = %d/n", fd);
     
    if (fd ==-1)
    {
        // print which type of error have in a code
        printf("Error Number % d\n", errno); 
         
        // print program detail "Success or failure"
        perror("Program");                 
    }
    return 0;
}
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
int main( )
{
int fd;
printf("\n This will create file");
fd=creat("t1.txt",0777);
printf("\nfd=%d",fd);
if(fd==-1)
{
printf("Error Number %d\n",errno);
}
return 0;
}
#include<stdio.h>
#include <fcntl.h>
int main()
{
  int fd, sz;
  char *c = (char *) calloc(100, sizeof(char));

  fd = open("f1.txt", O_RDONLY);
  if (fd = = -1)
   {
   perror("r1");
   exit(1);
   }
  sz = read(fd, c, 10);
  printf("called read(% d, c, 10).  returned that" " %d bytes  were read.\n", fd, sz);
  c[sz] = '\0';
  printf("Those bytes are as follows: % s\n", c);
  return 0;
}
#include<stdio.h>
#include <fcntl.h>
Int main( )
{
  int sz;
 
  int fd = open("f1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd ==-1)
  {
     perror("r1");
     exit(1);
  }

  sz = write(fd, "hello linux", strlen("hello linux"));

  printf("called write(%d, \"hello linux\", %d). It returned %d\n", fd, strlen("hello linux"), sz);

  close(fd);
  return 0;
}
#include<stdio.h>
#include <fcntl.h>
int main()
{
    int fd1 = open("f1.txt", O_RDONLY);
    if (fd1 = = -1)
    {
	perror("c1");
	exit(1);
    }
    printf("opened the fd = % d\n", fd1);

    // Using close system Call
    if (close(fd1) = = -1)
    {
	perror("c1");
	exit(1);
    }
    printf("closed the fd.\n");
    return 0;
}
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
 
int main()
{
        int file=0;
        if((file=open("testfile.txt",O_RDONLY)) < -1)
                return 1;
 
        char buffer[19];
        if(read(file,buffer,19) != 19)  return 1;
        printf("%s\n",buffer);
 
        if(lseek(file,10,SEEK_SET) < 0) return 1;
 
        if(read(file,buffer,19) != 19)  return 1;
        printf("%s\n",buffer);
 
        return 0;
}
#include<stdio.h>
int n,nf;
int in[100];
int p[50];
int hit=0;
int i,j,k;
int pgfaultcnt=0;

void getData()
{
    printf("\nEnter length of page reference sequence:");
    scanf("%d",&n);
    printf("\nEnter the page reference sequence:");
    for(i=0; i<n; i++)
        scanf("%d",&in[i]);
    printf("\nEnter no of frames:");
    scanf("%d",&nf);
}

void initialize()
{
    pgfaultcnt=0;
    for(i=0; i<nf; i++)
        p[i]=9999;
}

int isHit(int data)
{
    hit=0;
    for(j=0; j<nf; j++)
    {
        if(p[j]==data)
        {
            hit=1;
            break;
        }

    }

    return hit;
}
    
void lru()
{
    initialize();

    int least[50];
    for(i=0; i<n; i++)
    {

        printf("\nFor %d :",in[i]);

        if(isHit(in[i])==0)
        {

            for(j=0; j<nf; j++)
            {
                int pg=p[j];
                int found=0;
                for(k=i-1; k>=0; k--)
                {
                    if(pg==in[k])
                    {
                        least[j]=k;
                        found=1;
                        break;
                    }
                    else
                        found=0;
                }
                if(!found)
                    least[j]=-9999;
            }
            int min=9999;
            int repindex;
            for(j=0; j<nf; j++)
            {
                if(least[j]<min)
                {
                    min=least[j];
                    repindex=j;
                }
            }
            p[repindex]=in[i];
            pgfaultcnt++;

            for (k=0; k<nf; k++)
                if(p[k]!=9999)
                    printf(" %d",p[k]);
        }
        else
            printf("No page fault!");
    }
    printf("\nTotal no of page faults:%d",pgfaultcnt);
}


int main()
{
    getData();
    lru();
}
#include<stdio.h>

int size, base[100], limit[100], offset, flag, n;

int main() {
    printf("enter size of physical memory:");
    scanf("%d", &size);
    printf("enter no of segments:");
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        printf("enter limit of segment %d\n", i);
        scanf("%d", &limit[i]);
        if (limit[i] > size) {
            printf("\nlimit should not exceed physical memory\n");
            i--;
            continue;
        }
    }
    for (int i = 0; i < n; i++) {
        printf("enter base address of segment %d\n", i);
        scanf("%d", &base[i]);
        if (base[i] > size) {
            printf("\nbase address should not exceed physical memory\n");
            i--;
            continue;
        }
    }

    printf("segno\t\tbase\t\tlimit\n");
    for (int i = 0; i < n; i++) {
        printf("%d\t\t\t%d\t\t\t%d\n", i, base[i], limit[i]);
    }

    for (int i = 0; i < n; i++) {
        printf("seg %d occupied physical memory from %d to %d\n", i, base[i], base[i] + limit[i] - 1);
    }

    do {
        int j;
        printf("enter segment no");
        scanf("%d", &j);
        printf("enter offset");
        scanf("%d", &offset);
        printf("phy address %d\n", base[j] + offset);
        printf("to continue enter 1 else 0");
        scanf("%d", &flag);
    } while (flag != 0);

    return 0;
}
#include<stdio.h>
#include<stdlib.h>

int in[100], p[20], n, nf;

void getData();

void getData() {
    printf("enter number of seq:");
    scanf("%d", &n);
    printf("enter seq:");
    for (int i = 0; i < n; i++) {
        scanf("%d", &in[i]);
    }
    printf("enter no of frames:");
    scanf("%d", &nf);
}

int isHit(int x) {
    for (int i = 0; i < nf; i++) {
        if (x == p[i])
            return 1;
    }
    return 0;
}

void display() {
    printf("status:\n");
    for (int i = 0; i < nf; i++)
        if (p[i] != 9999)
            printf("%d ", p[i]);
    printf("\n");
}

void fifo() {
    int pgfault = 0;
    for (int i = 0; i < nf; i++)
        p[i] = 9999;

    for (int i = 0; i < n; i++) {
        printf("for %d\n", in[i]);
        if (isHit(in[i]) == 0) {
            int k;
            for (k = 0; k < nf - 1; k++)
                p[k] = p[k + 1];
            p[k] = in[i];
            pgfault++;
            display();
        } else {
            printf("page fault does not exist\n");
        }
    }
    printf("no of pgfaults:%d", pgfault);
}

int main() {
    getData();
    fifo();
}
#include<stdio.h>
int n,nf;
int in[100];
int p[50];
int hit=0;
int i,j,k;
int pgfaultcnt=0;

void getData()
{
    printf("\nEnter length of page reference sequence:");
    scanf("%d",&n);
    printf("\nEnter the page reference sequence:");
    for(i=0; i<n; i++)
        scanf("%d",&in[i]);
    printf("\nEnter no of frames:");
    scanf("%d",&nf);
}

int isHit(int data)
{
    hit=0;
    for(j=0; j<nf; j++)
    {
        if(p[j]==data)
        {
            hit=1;
            break;
        }

    }

    return hit;
}

void dispPages()
{
    for (k=0; k<nf; k++)
        if(p[k]!=9999)
            printf(" %d",p[k]);
}

void optimal()
{
    pgfaultcnt=0;
    for(i=0; i<nf; i++)
        p[i]=9999;
    
    int near[50];
    for(i=0; i<n; i++)
    {

        printf("\nFor %d :",in[i]);

        if(isHit(in[i])==0)
        {

            for(j=0; j<nf; j++)
            {
                int pg=p[j];
                int found=0;
                for(k=i; k<n; k++)
                {
                    if(pg==in[k])
                    {
                        near[j]=k;
                        found=1;
                        break;
                    }
                    else
                        found=0;
                }
                if(!found)
                    near[j]=9999;
            }
            int max=-9999;
            int repindex;
            for(j=0; j<nf; j++)
            {
                if(near[j]>max)
                {
                    max=near[j];
                    repindex=j;
                }
            }
            p[repindex]=in[i];
            pgfaultcnt++;

            dispPages();
        }
        else
            printf("No page fault");
    }
    printf("\nTotal no of page faults:%d",pgfaultcnt);  
}   

int main()
{
    getData();
    optimal();
}
*SYSTEM CALLS PROGRAMS*

Write()

#include<unistd.h>

int main()

{

	write(1,”Hello”,5)

}

read()

#include<unistd.h>

int main()

{

Char b[30];

read(0,b,10);

write(1,b,10);

}

Open 

#include <stdio.h>

#include <fcntl.h>

#include <unistd.h>

int main() {

    int fd;

    char *filename = "example.txt";

    

    // Open the file for reading and writing, create if it doesn't exist

    fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);

    printf("File opened successfully!\n");

    

    // Close the file

    close(fd);

    printf("File closed successfully!\n");

    

    return 0;

}

lseek

#include <stdio.h>

#include <fcntl.h>

#include <unistd.h>

int main() {

    int fd;

    off_t offset;

    // Open the file for reading

    fd = open("example.txt", O_RDONLY);

    

    // Move the file offset to a specific position (e.g., 100 bytes from the beginning)

    offset = lseek(fd, 100, SEEK_SET);

    

    // Close the file

    close(fd);

    

    return 0;

}

stat. This information includes details such as file size, permissions, inode number, timestamps, and more.

#include <stdio.h>

#include <sys/stat.h>

int main() {

    const char *filename = "example.txt";

    struct stat file_stat;

    // Call the stat system call to retrieve information about the file

    stat(filename, &file_stat);

    // Display file information

    printf("File Size: %ld bytes\n", file_stat.st_size);

    printf("File Permissions: %o\n", file_stat.st_mode & 0777);

    printf("Inode Number: %ld\n", file_stat.st_ino);

    return 0;

}

Open the current directory, Read directory entries

#include <stdio.h>

#include <dirent.h>

int main() {

    DIR *dir;

    struct dirent *entry;

    // Open the current directory

    dir = opendir(".");

    if (dir == NULL) {

        perror("opendir");

        return 1;

    }

    // Read directory entries

    while ((entry = readdir(dir)) != NULL) {

        printf("%s\n", entry->d_name);

    }

    // Close the directory

    closedir(dir);

    return 0;

}
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#define FIFO_NAME "myfifo"

int main() {

    int fd;

    char *message = "Hello from writer process!";

    

    // Create the FIFO if it does not exist

    if (mkfifo(FIFO_NAME, 0666) == -1) {

        perror("mkfifo");

        exit(EXIT_FAILURE);

    }

    // Open the FIFO for writing

    fd = open(FIFO_NAME, O_WRONLY);

    if (fd == -1) {

        perror("open");

        exit(EXIT_FAILURE);

    }

    // Write the message to the FIFO

    if (write(fd, message, strlen(message) + 1) == -1) {

        perror("write");

        close(fd);

        exit(EXIT_FAILURE);

    }

    printf("Writer: Wrote message to FIFO.\n");

    

    // Close the FIFO

    close(fd);

    return 0;

}
#include <stdio.h>

#include <unistd.h>

#include <string.h>

int main() {

    int fd[2]; // Array to hold the two ends of the pipe: fd[0] for reading, fd[1] for writing

    pid_t pid;

    char writeMessage[] = "Hello from parent process!";

    char readMessage[100];

    // Create the pipe

    if (pipe(fd) == -1) {

        perror("pipe failed");

        return 1;

    }

    // Fork a child process

    pid = fork();

    if (pid < 0) {

        perror("fork failed");

        return 1;

    } else if (pid > 0) {

        // Parent process

        close(fd[0]); // Close the reading end of the pipe in the parent

        // Write a message to the pipe

        write(fd[1], writeMessage, strlen(writeMessage) + 1);

        close(fd[1]); // Close the writing end of the pipe after writing

        // Wait for child process to finish

        wait(NULL);

    } else {

        // Child process

        close(fd[1]); // Close the writing end of the pipe in the child

        // Read the message from the pipe

        read(fd[0], readMessage, sizeof(readMessage));

        printf("Child process received: %s\n", readMessage);

        close(fd[0]); // Close the reading end of the pipe after reading

    }

return 0;

}
Why we love Cookies
We use cookies to try and give you a better experience in Freshdesk.

You can learn more about what kind of cookies we use, why, and how from our Privacy Policy. If you hate cookies, or are just on a diet, you can disable them altogether too. Just note that the Freshdesk service is pretty big on some cookies (we love the choco-chip ones), and some portions of Freshdesk may not work properly if you disable cookies.

We’ll also assume you agree to the way we use cookies and are ok with it as described in our Privacy Policy, unless you choose to disable them altogether through your browser.

#include <stdio.h>
int main() {
    int n, total_waiting_time = 0, total_turnaround_time = 0;
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    int arrival_time[n], burst_time[n], waiting_time[n], turnaround_time[n], temp;
    for (int i = 0; i < n; i++) {
        printf("Enter arrival time and burst time for process %d: ", i + 1);
        scanf("%d %d", &arrival_time[i], &burst_time[i]);
    }
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (burst_time[j] > burst_time[j + 1]) {
                temp = burst_time[j];
                burst_time[j] = burst_time[j + 1];
                burst_time[j + 1] = temp;

                temp = arrival_time[j];
                arrival_time[j] = arrival_time[j + 1];
                arrival_time[j + 1] = temp;
            }
        }
    }
 printf("Process\tWaiting Time\tTurnaround Time\n");
    int current_time = 0;
    for (int i = 0; i < n; i++) {
        waiting_time[i] = current_time - arrival_time[i] < 0 ? 0 : current_time - arrival_time[i];
        turnaround_time[i] = waiting_time[i] + burst_time[i];
        total_waiting_time += waiting_time[i];
        total_turnaround_time += turnaround_time[i];
        printf("%d\t%d\t\t%d\n", i + 1, waiting_time[i], turnaround_time[i]);
        current_time += burst_time[i];
    }
    printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);
    printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
    return 0;
}
#include <stdio.h>
int main() {
    int n, total_waiting_time = 0, total_turnaround_time = 0;
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    int arrival_time[n], burst_time[n], waiting_time[n], turnaround_time[n];
    for (int i = 0; i < n; i++) {
        printf("Enter arrival time and burst time for process %d: ", i + 1);
        scanf("%d %d", &arrival_time[i], &burst_time[i]);
    }
    printf("Process\tarrivaltime\tbursttime\tWaiting Time\tTurnaround Time\n");
    int current_time = 0;
    for (int i = 0; i < n; i++) {
        waiting_time[i] = current_time - arrival_time[i] < 0 ? 0 : current_time - arrival_time[i];
        turnaround_time[i] = waiting_time[i] + burst_time[i];
        total_waiting_time += waiting_time[i];
        total_turnaround_time += turnaround_time[i];
        printf("%d\t\t\t%d\t\t\t%d\t\t\t%d\t\t\t\t%d\n",arrival_time[i],burst_time[i], i + 1, waiting_time[i], turnaround_time[i]);
        current_time += burst_time[i];
    }
    printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);
    printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
    return 0;
}
const XLSX = require("xlsx");
const fs = require("fs");
const path = require("path");

const filePath = "3Status.xlsx"; //File path of excel

const workbook = XLSX.readFile(filePath);
const worksheet = workbook.Sheets["Sheet0"];
const data = XLSX.utils.sheet_to_json(worksheet);
const folderPath = "test"; //File path of git TCs
const filePaths = [];

function getFilePaths(directory) {
  const items = fs.readdirSync(directory);
  for (const item of items) {
    const itemPath = path.join(directory, item);
    if (fs.statSync(itemPath).isDirectory()) {
      getFilePaths(itemPath);
    } else {
      filePaths.push(itemPath);
    }
  }
}
getFilePaths(folderPath);
function newProc(filePaths, keyword) {
  let flag1 = false;
  for (const singlePath of filePaths) {
    const fileContent = fs.readFileSync(singlePath, "utf-8");
    //console.log("fileContent is ", fileContent)
    if (fileContent.includes(keyword)) {
      flag1 = true;
      //console.log(`Keyword '${keyword}' found in file: ${singlePath}`);
    }
  }
  return flag1;
}
let notAutomated = [];
for (const i of data) {
  let flag = false;
  let check = newProc(filePaths, i["TestRail Id"]);
  //console.log("check is ", check)
  if (check == false) {
    notAutomated.push(i["TestRail Id"]);
    //console.log(`${i["TestRail Id"]} is not automated`)
  }
}
console.log(`notAutomated TCs are `, notAutomated);
console.log("No.of notAutomated are ", notAutomated.length);

//Storing the notAutomated ones in the excel file
const values = ["NonAutomatedTestRailIds", ...notAutomated];
const workbook1 = XLSX.utils.book_new();
const worksheet1 = XLSX.utils.aoa_to_sheet([]);
values.forEach((value, index) => {
  const cellAddress = XLSX.utils.encode_cell({ c: 0, r: index }); // column 0, row index
  worksheet1[cellAddress] = { v: value };
});
worksheet1["!ref"] = XLSX.utils.encode_range({
  s: { c: 0, r: 0 },
  e: { c: 0, r: values.length - 1 },
});
XLSX.utils.book_append_sheet(workbook1, worksheet1, "Sheet1");
XLSX.writeFile(workbook1, "NonAutomatedTestRailIdsFile.xlsx");
import java.util.*;
public class HeapSort {
    public static void heapSort(Integer[] array){
        buildMaxHeap(array);

        for(int i=array.length-1;i>0;i--){
            swap(array,0,i);
            maxHeapify(array,i,0);
        }
    }
    public static void buildMaxHeap(Integer[] array){
        int n=array.length;
        for(int i=n/2-1;i>=0;i--){
            maxHeapify(array,n,i);
        }
    }
    private static void maxHeapify(Integer[] array,int heapsize,int rootindex){
        int largest=rootindex;
        int leftchild=2*rootindex+1;
        int rightchild=2*rootindex+2;

        if(leftchild<heapsize && array[leftchild]>array[largest]){
            largest=leftchild;
        }
        if(rightchild<heapsize && array[rightchild]>array[largest]){
            largest=rightchild;
        }
        if(largest!=rootindex){
            swap(array,rootindex,largest);

            maxHeapify(array, heapsize, rootindex);
        }
    }
    private static void swap(Integer[] array,int i,int j){
        int temp=array[i];
        array[i]=array[j];
        array[j]=temp;
    }
    public static void main(String args[]){
        Integer[] array={12,11,13,5,6,7};
        System.out.println("Original array:"+Arrays.toString(array));

        heapSort(array);

        System.out.println("Sorted array:"+Arrays.toString(array));
    }
}
// C Program for Message Queue (Reader Process) 
#include <stdio.h> 
#include <sys/ipc.h> 
#include <sys/msg.h> 

// structure for message queue 
struct mesg_buffer { 
	long mesg_type; 
	char mesg_text[100]; 
} message; 

int main() 
{ 
	key_t key; 
	int msgid; 

	// ftok to generate unique key 
	key = ftok("progfile", 65); 

	// msgget creates a message queue 
	// and returns identifier 
	msgid = msgget(key, 0666 | IPC_CREAT); 

	// msgrcv to receive message 
	msgrcv(msgid, &message, sizeof(message), 1, 0); 

	// display the message 
	printf("Data Received is : %s \n", 
					message.mesg_text); 

	// to destroy the message queue 
	msgctl(msgid, IPC_RMID, NULL); 

	return 0; 
} 
// C Program for Message Queue (Writer Process) 
#include <stdio.h> 
#include <sys/ipc.h> 
#include <sys/msg.h> 
#define MAX 10 

// structure for message queue 
struct mesg_buffer { 
	long mesg_type; 
	char mesg_text[100]; 
} message; 

int main() 
{ 
	key_t key; 
	int msgid; 

	// ftok to generate unique key 
	key = ftok("progfile", 65); 

	// msgget creates a message queue 
	// and returns identifier 
	msgid = msgget(key, 0666 | IPC_CREAT); 
	message.mesg_type = 1; 

	printf("Write Data : "); 
	fgets(message.mesg_text,MAX,stdin); 

	// msgsnd to send message 
	msgsnd(msgid, &message, sizeof(message), 0); 

	// display the message 
	printf("Data send is : %s \n", message.mesg_text); 

	return 0; 
} 
#include<stdio.h>  
#include<stdlib.h>  
#include<unistd.h>  
#include<sys/shm.h>  
#include<string.h>  
int main()  
{  
int i;  
void *shared_memory;  
char buff[100];  
int shmid;  
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);   
//creates shared memory segment with key 2345, having size 1024 bytes. IPC_CREAT is used to create the shared segment if it does not exist. 0666 are the permissions on the shared segment  
printf("Key of shared memory is %d\n",shmid);  
shared_memory=shmat(shmid,NULL,0);   
//process attached to shared memory segment  
printf("Process attached at %p\n",shared_memory);   
//this prints the address where the segment is attached with this process  
printf("Enter some data to write to shared memory\n");  
read(0,buff,100); //get some input from user  
strcpy(shared_memory,buff); //data written to shared memory  
printf("You wrote : %s\n",(char *)shared_memory);  
}  
#include<stdio.h>  
#include<stdlib.h>  
#include<unistd.h>  
#include<sys/shm.h>  
#include<string.h>  
int main()  
{  
int i;  
void *shared_memory;  
char buff[100];  
int shmid;  
shmid=shmget((key_t)2345, 1024, 0666);  
printf("Key of shared memory is %d\n",shmid);  
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment  
printf("Process attached at %p\n",shared_memory);  
printf("Data read from shared memory is : %s\n",(char *)shared_memory);  
}  
/*** Insert into tablet + mobile CSS for ROW ***/
display:flex;
flex-wrap:wrap;

/*** Insert into tablet + mobile CSS for the second COLUMN ***/
order:-1;
#include<stdio.h>
int n;
int main()
{
int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s;
int count=1,pf=0,p=0;
float pfr;
printf("Enter maximum limit of the sequence: ");
scanf("%d",&max);
printf("\nEnter the sequence: ");
 for(i=0;i<max;i++)
scanf("%d",&seq[i]);
 printf("\nEnter no. of frames: ");
 scanf("%d",&n);
fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1; p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j]) flag=0;
}
if(flag!=0)
{
 fr[count]=seq[i];
 printf("%d\t",fr[count]);
 count++;
pf++;

}
i++;
}
 
printf("\n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
{
if(seq[i]==fr[j])
flag=0;
}
if(flag!=0)

{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
{
if(seq[k]==m)
{
    pos[j]=k;
     break;

 }
else
pos[j]=1;

}
}
for(k=0;k<n;k++)
{
if(pos[k]==1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
 if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==1)
{
s=k;
 break;
}
}
}
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
pf++;
printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\nThe no. of page faults are %d",pf);
printf("\nPage fault rate %f",pfr);
getch();
}
int findmax(int a[])
{
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
  max=a[i];
   k=i;
}
}
return k;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
//clrscr();
printf("Enter the length of reference string -- ");
 scanf("%d",&n);
printf("Enter the reference string -- ");
 for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames -- ");
 scanf("%d",&f);
for(i=0;i<f;i++)

{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
 for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
 next++;
}

}
if(flag[i]==0)
{

if(i<f)
{  m[i]=rs[i];
   count[i]=next;
   next++;
 }
else
{ min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
  min=j;

m[min]=rs[i];
 count[min]=next;
  next++;


}
pf++;
}

for(j=0;j<f;j++)
printf("%d\t", m[j]);
 if(flag[i]==0)
printf("PF No. -- %d" , pf);
 printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
 getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
printf("\n Enter the length of reference string -- ");
scanf("%d",&n);
printf("\n Enter the reference string -- ");
 for(i=0;i<n;i++)
scanf("%d",&rs[i]);
 printf("\n Enter no. of frames -- ");
 scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;

printf("\n The Page Replacement Process is -- \n");
 for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
  if(m[k]==rs[i])
  break;

}
if(k==f)
{
m[count++]=rs[i];
pf++;

}

for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf);
getch();
}
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
   int n;
   void producer();
   void consumer();
   int wait(int);
   int signal(int);
   printf("\n 1.Producer \n 2.Consumer \n 3.Exit");
   while(1)
   {
      printf("\nEnter your choice:");
      scanf("%d",&n);
      switch(n)
      {
         case 1:
                 if((mutex==1)&&(empty!=0))
                    producer();
                 else
                    printf("Buffer is full");
     break;
         case 2:
             if((mutex==1)&&(full!=0))
    consumer();
    else
        printf("Buffer is empty");
       break;
         case 3:
    exit(0);
    break;
      }
   }
}
int wait(int s)
{
   return (--s);
}
int signal(int s)
{
   return(++s);
}
void producer()
{
   mutex=wait(mutex);
   full=signal(full);
   empty=wait(empty);
   x++;
   printf("Producer produces the item %d\n",x);
   mutex=signal(mutex);
}
void consumer()
{
   mutex=wait(mutex);
   full=wait(full);
   empty=signal(empty);
   printf("Consumer consumes item %d\n",x);
   x--;
   mutex=signal(mutex);
}
#include <stdio.h>
int main() {
    int n, m, i, j, k;

    // Get the number of processes and resources from the user
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    printf("Enter the number of resources: ");
    scanf("%d", &m);

    int alloc[n][m], max[n][m], avail[m];

    // Get the Allocation Matrix from the user
    printf("Enter the Allocation Matrix:\n");
    for (i = 0; i < n; i++) {
        printf("Process %d:\n", i);
        for (j = 0; j < m; j++) {
            scanf("%d", &alloc[i][j]);
        }
    }

    // Get the Maximum Matrix from the user
    printf("Enter the Maximum Matrix:\n");
    for (i = 0; i < n; i++) {
        printf("Process %d:\n", i);
        for (j = 0; j < m; j++) {
            scanf("%d", &max[i][j]);
        }
    }

    // Get the Available Resources from the user
    printf("Enter the Available Resources:\n");
    for (i = 0; i < m; i++) {
        scanf("%d", &avail[i]);
    }

    int f[n], ans[n], ind = 0;
    for (k = 0; k < n; k++) {
        f[k] = 0;
    }

    int need[n][m];
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            need[i][j] = max[i][j] - alloc[i][j];
        }
    }

    int y = 0;
    for (k = 0; k < n; k++) {
        for (i = 0; i < n; i++) {
            if (f[i] == 0) {
                int flag = 0;
                for (j = 0; j < m; j++) {
                    if (need[i][j] > avail[j]) {
                        flag = 1;
                        break;
                    }
                }

                if (flag == 0) {
                    ans[ind++] = i;
                    for (y = 0; y < m; y++) {
                        avail[y] += alloc[i][y];
                    }
                    f[i] = 1;
                }
            }
        }
    }

    int flag = 1;
    for (i = 0; i < n; i++) {
        if (f[i] == 0) {
            flag = 0;
            printf("The system is not in a safe state.\n");
            break;
        }
    }

    if (flag == 1) {
        printf("The system is in a safe state.\nSafe sequence is: ");
        for (i = 0; i < n - 1; i++) {
            printf("P%d -> ", ans[i]);
        }
        printf("P%d\n", ans[n - 1]);
    }

    return 0;
}
#include <stdio.h>

int main() {
    int n, i, j, temp, sum_wait = 0, sum_turnaround = 0;
    float avg_wait, avg_turnaround;
    int priority[20], bt[20], wt[20], tat[20];

    printf("Enter the number of processes: ");
    scanf("%d", &n);

    // Input burst times and priorities for each process
    printf("Enter burst times and priorities for each process:\n");
    for (i = 0; i < n; i++) {
        printf("Process %d: ", i + 1);
        scanf("%d%d", &bt[i], &priority[i]);
    }

    // Sort processes based on priority (ascending order)
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if (priority[i] > priority[j]) {
                temp = priority[i];
                priority[i] = priority[j];
                priority[j] = temp;
                temp = bt[i];
                bt[i] = bt[j];
                bt[j] = temp;
            }
        }
    }

    // Calculate waiting time for each process
    wt[0] = 0; // Waiting time for first process is zero
    for (i = 1; i < n; i++) {
        wt[i] = wt[i - 1] + bt[i - 1];
        sum_wait += wt[i];
    }

    // Calculate turnaround time for each process
    for (i = 0; i < n; i++) {
        tat[i] = wt[i] + bt[i];
        sum_turnaround += tat[i];
    }

    // Calculate average waiting time and average turnaround time
    avg_wait = (float)sum_wait / n;
    avg_turnaround = (float)sum_turnaround / n;

    // Print process details
    printf("\nProcess\tBT\tPriority\tWT\tTAT\n");
    for (i = 0; i < n; i++) {
        printf("P%d\t%d\t%d\t\t%d\t%d\n", i + 1, bt[i], priority[i], wt[i], tat[i]);
    }

    // Print average waiting time and average turnaround time
    printf("\nAverage Waiting Time: %.2f\n", avg_wait);
    printf("Average Turnaround Time: %.2f\n", avg_turnaround);

    return 0;
}
star

Fri Jun 07 2024 04:57:31 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:56:58 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:56:22 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:54:34 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:53:54 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:52:04 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:51:37 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:50:56 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:50:13 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:49:24 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:47:25 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Fri Jun 07 2024 04:38:30 GMT+0000 (Coordinated Universal Time)

@exam123

star

Fri Jun 07 2024 04:10:57 GMT+0000 (Coordinated Universal Time) https://atcoder.jp/contests/dp/submissions/54289370

@devdutt

star

Fri Jun 07 2024 03:25:40 GMT+0000 (Coordinated Universal Time)

@dbms

star

Fri Jun 07 2024 03:25:10 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 23:54:22 GMT+0000 (Coordinated Universal Time)

@gabriellesoares

star

Thu Jun 06 2024 20:54:04 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:53:09 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:51:53 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:47:12 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:46:34 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:45:49 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:44:10 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:43:21 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:42:07 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:40:06 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:39:10 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:37:42 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 20:36:53 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 18:52:24 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 18:51:53 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 18:51:26 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 18:11:03 GMT+0000 (Coordinated Universal Time) https://bbbaccountabilityprogram.freshdesk.com/support/home

@curtisbarry

star

Thu Jun 06 2024 18:06:13 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 17:50:23 GMT+0000 (Coordinated Universal Time)

@prabhas

star

Thu Jun 06 2024 17:34:53 GMT+0000 (Coordinated Universal Time)

@Akhil_preetham #javascript #nodejs

star

Thu Jun 06 2024 17:04:43 GMT+0000 (Coordinated Universal Time) https://uptimerobot.com/api/

@curtisbarry

star

Thu Jun 06 2024 17:04:21 GMT+0000 (Coordinated Universal Time) https://uptimerobot.com/api/

@curtisbarry

star

Thu Jun 06 2024 16:55:44 GMT+0000 (Coordinated Universal Time)

@adsj

star

Thu Jun 06 2024 16:54:33 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:53:46 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:51:53 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:51:24 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:50:42 GMT+0000 (Coordinated Universal Time)

@KaiTheKingRook

star

Thu Jun 06 2024 16:40:52 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:40:16 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:39:55 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:39:21 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:38:55 GMT+0000 (Coordinated Universal Time)

@dbms

star

Thu Jun 06 2024 16:38:32 GMT+0000 (Coordinated Universal Time)

@dbms

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension