Question

#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void...

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<time.h>

void insertionSort(int arr[], int n);
void merge(int a[], int l1, int h1, int h2);

void mergeSort(int a[], int l, int h)
{
int i, len=(h-l+1);
//Using insertion sort for small sized array
if (len<=5)
{
insertionSort(a+l, len);
return;
}
pid_t lpid,rpid;
lpid = fork();
if(lpid<0)
{
//Lchild proc not created
perror("Left Child Proc. not created\n");
_exit(-1);
}
else if (lpid==0)
{
mergeSort(a,l,l+len/2-1);
_exit(0);
}
else
{
rpid = fork();
if (rpid<0)
{
// Rchild proc not created
perror("Right Child Proc. not created\n");
_exit(-1);
}
else if(rpid==0)
{
mergeSort(a,l+len/2,h);
_exit(0);
}
}
int status;
//Wait for child processes to finish
waitpid(lpid, &status, 0);
waitpid(rpid, &status, 0);
//merge the sorted svbarrays
merge(a, l, l+len/2-1, h);
}

/* Function to sort an array using Insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for(i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */
while(j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j -1;
}
arr[j+1] = key;
}
}

//Method to merge sorted subarrays
void merge(int a[], int l1, int h1, int h2)
{
// We Can directly copy the sorted elements in the final array, no need for a temporary sorted array.
int count=h2-l1+1;
int sorted[count];
int i=l1, k=h1+1, m=0;
while (i<=h1 && k<=h2)
{
if (a[i]<a[k])
sorted[m++]=a[i++];
else if (a[k] < a[i])
sorted[m++] = a[k++];
else if (a[i] == a[k])
{
sorted[m++] = a[i++];
sorted[m++] = a[k++];
}
}
while (i<=h1)
sorted[m++] = a[i++];

while (k<=h2)
sorted[m++] = a[k++];

int arr_count = l1;
for( i = 0; i<count; i++, l1++)
a[l1] = sorted[i];
}

// To check if array is actually sorted or not
void isSorted(int arr[], int len)
{
if(len == 1)
{
printf("Sorting done successfully\n");
return;
}
int i;
for(i = 1; i<len; i++)
{
if (arr[i]<arr[i-1])
{
printf("Sorting not done\n");
return;
}
}
printf("Sorting done successfully\n");
return;
}

// To fill random values in array for testing purpose
void fillData(int a[], int len)
{
// Create random arrays
int i;
for (i=0; i<len; i++)
a[i] = rand();
return;
}

// Driver code
int main()
{
int shmid;
key_t key = IPC_PRIVATE;
int *shm_array;
int length = 128;
//Calculate segment length
size_t SHM_SIZE = sizeof(int)*length;
// Create the segment.
if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0)
{
perror("shmget");
_exit(1);
}

// Now we attach the segment to our data space.
if ((shm_array = shmat(shmid, NULL, 0)) == (int *) -1)
{
perror("shmat");
_exit(1);
}

// Create a random array of given length
srand(time(NULL));
fillData(shm_array, length);

// Sort the created array
mergeSort(shm_array, 0, length-1);

//Check 1f array Is sorted or not
isSorted(shm_array, length);

/* Detach from the shared memory now that we are done using it. */
if (shmdt(shm_array) == -1)
{
perror("shmdt");
_exit(1);
}
/* Delete the shared memory segment. */
if (shmctl(shmid, IPC_RMID, NULL) == -1)
{
perror("shmctl");
_exit(1);
}
return 0;
}

WHAT IS WRONG WITH THIS CODE??

ERROR IM GETTING :

error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
154 | if ((shm_array = shmat(shmid, NULL, 0)) == (int *) -1)
| ~~~~~^~~~~~~~~~~~~~~~
| |
| void*

0 0
Add a comment Improve this question Transcribed image text
Answer #1


PLEASE FIND THE ANSWER(S) and EXPLANATION BELOW.

The question says there is a type conversion error.

error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
154 | if ((shm_array = shmat(shmid, NULL, 0)) == (int *) -1)
| ~~~~~^~~~~~~~~~~~~~~~
| |
| void*

But, there is no error in the code.

I am able to run the code as we can see it below.

For the reference, I am attaching some piece of code to print the array.

==========// print the data once
printf("The sorted array is: ");
for(int i=0;i<length;i++)
{
printf("%d, ",shm_array[i]);
}

===========

Please run it properly.

If there are any doubts, comment down here. We are right here to help you. Happy Learning..!! PLEASE give an UPVOTE I Have Pu

Add a comment
Know the answer?
Add Answer to:
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main()...

    What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int x = 5; int y = 2; int z = 30; x = fork(); y = fork(); if (x != 0) printf("Type 1\n"); if (y != 0) printf("Type 2\n"); z = fork(); if (x > 0 || y > 0 || z > 0) printf("Type 3\n"); if (x == 0 && y == 0 && z != 0) printf("Type 4\n"); if (x...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void...

    ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void func(int **a) {int tmp; for (int i = 0; i < size; i++) {for (int j = i; j < size, j++) {tmp = *(*(a+i)+j); *(*(a+i)+j) = *(*(a+j)+i); *(*(a+j)+i) = tmp;}}} int main() {int **arr = malloc(sizeof(int*) * size); for(int i = 0; i < size; i++) {arr[i] = malloc(sizeof(int) * size); for (int j = 0; j < size, j++) arr[i][j] = 2*i...

  • Implement merge sort and merge #include <iostream> using namespace std; void * merge(int arr[], int start1, int end1, int start2, int end2){ int * combined = new int[end2-start1 + 1];         ...

    Implement merge sort and merge #include <iostream> using namespace std; void * merge(int arr[], int start1, int end1, int start2, int end2){ int * combined = new int[end2-start1 + 1];             } void mergeSort(int arr[], int start, int end){ //base case: down to 1 item, do nothing //recursive case: //MergeSort(left) //MergeSort(right) //Merge(left, right) int m = (end - start) / 2; if(start==end){ } else { mergeSort(arr, start, m); mergeSort(arr, m+1, end); merge(arr, start, m, m+1, end); } } void fill(int arr[],...

  • With explanation Please. #include #include <sys/types.h> <unistd.h> int main void ) fork fork fork execip (...

    With explanation Please. #include #include <sys/types.h> <unistd.h> int main void ) fork fork fork execip ( "/bin/ls", return 0 /* Line A /* Line B/ /*Line C/ /. Line D ./ "ls", NULL); Answer the following: (a) Including the initial parent process, how many processes are created by the program? (b) Answer (a) assuming that lines C and D are interchanged in the program (c) Answer (a) assuming that lines B and D are interchanged (instead of C and D)...

  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT