コード例 #1
0
ファイル: noOfOnes.c プロジェクト: soorajparemal/TestANswers
int main(void)
{
  unsigned int n;
  printf("Enter a positive integer: ");
  scanf("%u", &n);
  printf("%d\n", countSetBits(n));
}
コード例 #2
0
ファイル: setbit.c プロジェクト: mayankiitj/Mission2k15
/* Program to test function countSetBits */
int main()
{
   int n,i,j=1,res,k=0;
   scanf("%d",&n);
   for(i=1;i<n;i++){
    j=2*j;
    if(n<j)
    {k=i+1;
     break;
}
else if(n==j)
{k=i+2;
break;
}
}

   int arr[n][2];
   for(i=0;i<n;i++)
  {
     res=countSetBits(i+1);
     arr[i][1]=i+1;
     arr[i][0]=res; 
}
  bucketsort(arr,k);
    return 0;
}
コード例 #3
0
int main()
{
    int n, k;
    printf("\nEnter n\n");
    scanf("%d", &n);

    int count = countSetBits(n);
    if (count == 1)
        printf("\nThe number %d is power of 2\n", n);
    else
        printf("\nThe number %d is not power of 2\n", n);
    printf("\n");
    return 0;
}
コード例 #4
0
ファイル: master.cpp プロジェクト: sjain39/Projects
int main(int argc, char** argv)
{
	printf("\n[HW2] IPC and Synchronization\nName: Shubham Jain\nEmail: [email protected]\nNetID: sjain39\n-------------------------------\n");
	if(argc < 5 || argc > 7)
	{
		printf("Usage:\nmaster nBuffers nWorkers sleepMin sleepMax [ randSeed ] [ -lock | -nolock ]\n");
		exit(-1);
	}
	int nBuffers=0, nWorkers=0, randseed=0, i=0, j=0, childStatus=0, workerStatus=0, semID=-1;
	nBuffers = atoi(argv[1]);
	nWorkers = atoi(argv[2]);
	float sleepMin, sleepMax;
	sleepMin = atof(argv[3]);
	sleepMax = atof(argv[4]);
	
	checkInput(nBuffers, nWorkers, sleepMin, sleepMax);
	if(argc==6)
	{
		if(!strcmp(argv[5],"-lock") || !strcmp(argv[5],"lock"))
		{
			printf("Implementing semaphores...\n");
			if((semID = semget(IPC_PRIVATE, nBuffers, IPC_CREAT | 0600)) < 0)
			{
				perror("semget() failed in master");
				exit(-1);
			}
		}
		else
			randseed = atoi(argv[5]);
	}
	if(argc==7)
	{
		randseed = atoi(argv[5]);
		if(!strcmp(argv[6],"-lock") || !strcmp(argv[6],"lock"))
		{
			 printf("Implementing semaphores...\n");
			 if((semID = semget(IPC_PRIVATE, nBuffers, IPC_CREAT | 0600)) < 0)
			 {
				 perror("semget() failed in master");
				 exit(-1);
			 }
		}
	}
		

	int shmid=0, msgQue=0, *shm=NULL;

	if ((shmid = shmget(IPC_PRIVATE, nBuffers*sizeof(int), IPC_CREAT | 0600)) < 0)
	{
		perror("shmget() error");
		exit(-1);
	}

	if((msgQue = msgget(IPC_PRIVATE, IPC_CREAT | 0600)) < 0)
	{
		perror("msgget failed");
		exit(-1);
	}

	char *sort_args[] = {"sort", "-nr", NULL};

	int pipe1[2]; //toChild
	int pipe2[2]; //toParent

	if(pipe(pipe1) || pipe(pipe2))
	{
		perror("pipe() failed");
		exit(-1);
	}

	pid_t pid1 = fork();

	if(pid1 < 0)
	{
		perror("fork() failed for sort");
		exit(-1);
	}
	if(pid1 == 0)
	{ 
		close(pipe1[1]);
		close(pipe2[0]);

		dup2(pipe1[0], STDIN);
		dup2(pipe2[1], STDOUT);

		close(pipe1[0]);
		close(pipe2[1]);

		execvp("sort", sort_args);
   
		perror("execvp() failed for sort");
		exit(-1);
	}
	else
	{
		close(pipe1[0]);
		close(pipe2[1]);

		if(randseed == 0)
			srand(time(NULL));
		else
			srand(randseed);

		char writeBuffer[256] = {'\0'};

		printf("Random sleepTimes sent to child:\n================================\n");
		for(i=0; i<nWorkers; i++)
		{
			float random = (rand()/(float) RAND_MAX) * (sleepMax-sleepMin) + sleepMin;
			sprintf(writeBuffer+j,"%f\n", random);
			printf("%s", writeBuffer+j);
			j=strlen(writeBuffer);
		}

		write(pipe1[1], writeBuffer, strlen(writeBuffer));

		close(pipe1[1]);

		if(wait(&childStatus) < 0 )
		{
			perror("wait() failed for sort");
			exit(-1);
		}
		
		float sleepTimes[nWorkers];
		char *readBuffer;
		readBuffer = (char*)malloc((strlen(writeBuffer)+1)*sizeof(char));
		j=0;
		printf("\nSorted sleepTimes recieved from child:\n======================================\n");
		while(j<nWorkers)
		{
			i=0;
			char c;
			while(read(pipe2[0],&c,1)==1 && c!='\n'){
			readBuffer[i++]=c;
			}
			readBuffer[i]='\0';
			printf("%s\n", readBuffer);
			sleepTimes[j] = atof(readBuffer);
			j++;
		}
		close(pipe2[0]);
		printf("\n");

		if(semID != -1)
		{
			for (i=0; i<nBuffers; i++)
			{
				union semun sem_union;
				sem_union.val = 1;
				if(semctl(semID, i, SETVAL, sem_union) < 0)
				{
					perror("semctl() failed");
					exit(-1);
				}
			}
		}

		for(i=0; i<nWorkers; i++)
		{ //fork workers
			int pid2 = fork();
			if(pid2 < 0)
			{
				perror("fork() failed");
				exit(-1);
			}
			if(pid2 == 0)
			{
				char workerID[10];
				sprintf(workerID, "%d", i+1);

				char numBuffers[10];
				sprintf(numBuffers, "%d", nBuffers);

				char sleepTime[10];
				sprintf(sleepTime, "%f", sleepTimes[i]);

				char msgQueID[10];
				sprintf(msgQueID, "%d", msgQue);

				char shmID[10];
				sprintf(shmID, "%i", shmid);

				if(semID!=-1)
				{
					char semId[10];
					sprintf(semId, "%d", semID);
					char *worker_args[] = {"worker",  workerID, numBuffers, sleepTime, msgQueID, shmID, semId, NULL};
					execv("worker", worker_args);
				}
				else
				{
					 char *worker_args[] = {"worker",  workerID, numBuffers, sleepTime, msgQueID, shmID, NULL};
					 execv("worker", worker_args);
				}
				perror("execv() failed for worker");
				exit(-1);
			}
			else
				continue;
		}
		int nleft = nWorkers;
		int readErrors=0, writeErrors=0;
		while(nleft)
		{ //rcv worker messages
			struct msgbuffer rcvd;
			if(msgrcv(msgQue, &rcvd, sizeof(rcvd), 0, 0) < 0)
			{
				perror("msgrcv() failed");
				exit(-1);
			}
			printf("%s\n",rcvd.mtext);
			switch(rcvd.mtype)
			{
				case 2: readErrors++;
					break;
				case 3: if(wait(&workerStatus) <0)
					{
						perror("wait() failed for worker");
						exit(-1);
					}
					nleft--;
					break;
				default: break;
			}
		}
		printf("\nAll workers have finished execution.\n");

		int expectedVal = (1<<nWorkers)-1;
		printf("\nExpected value in all buffers = %d\n", expectedVal);
		printf("Actual value in all buffers is as follows:\n");
		if ((shm = (int*)shmat(shmid, NULL, 0)) == (int*) -1)
		{
			perror("shmat() error");
			exit(-1);
		}
		for(i=0; i<nBuffers; i++)
			printf("Buffer[%d] = %d\n", i, shm[i]);
		
		//check for bad bits
		for(i=0; i<nBuffers; i++)
		{
			int diffbits = shm[i] ^ expectedVal;
			int setBits = countSetBits(diffbits);
			if(setBits)
			{
				writeErrors++;
				printf("=========================================\nError in Buffer[%d]. No. of Bad Bits = %d\n", i, setBits);
			}
			int iter;
			for (j=1, iter=1; j<256; j<<=1, iter++)
				if (diffbits & j)
					printf("Bad Bit set at bit_index[%d] in Buffer[%d]\n", iter, i);
		}

		printf("\n<=====================>\nTotal Read Errors = %d\nTotal Write Errors = %d\n<=====================>\n", readErrors, writeErrors);
		if(msgctl(msgQue, IPC_RMID, NULL)){

			perror("msgctl() failed");
			exit(-1);
		}

		if(shmdt(shm) < 0)
		{
			perror("shmdt() failed");
			exit(-1);
		}
		
		if(shmctl(shmid, IPC_RMID, NULL) < 0)
		{
			perror("shmctl() failed");
			exit(-1);
		}
		if(semID!=-1)
		{
			if(semctl(semID, 0, IPC_RMID) == -1)
			{
				perror("semctl() failed");
				exit(-1);
			}
		}
	}
	return 0;
}
コード例 #5
0
    CoreCount getCpuCoreCount()
    {
        LPFN_GLPI glpi;
        BOOL done = FALSE;
        CoreCount coreCount;
        PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
        PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
        DWORD returnLength = 0;
        DWORD logicalProcessorCount = 0;
        DWORD processorCoreCount = 0;
        DWORD byteOffset = 0;

        glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
                                         "GetLogicalProcessorInformation");

        if (NULL == glpi)
        {
            return coreCount;
        }

        while (!done)
        {
            DWORD rc = glpi(buffer, &returnLength);

            if (FALSE == rc)
            {
                if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
                {
                    if (buffer)
                    {
                        free(buffer);
                    }

                    buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);

                    if (NULL == buffer)
                    {
                        return coreCount;
                    }
                }
                else
                {
                    return coreCount;
                }
            }
            else
            {
                done = TRUE;
            }
        }

        ptr = buffer;

        while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength)
        {
            if (RelationProcessorCore == ptr->Relationship)
            {
                processorCoreCount++;

                // A hyperthreaded core supplies more than one logical processor.
                logicalProcessorCount += countSetBits(ptr->ProcessorMask);
            }

            byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
            ptr++;
        }

        free(buffer);
        coreCount.m_cpuCoreCount = processorCoreCount;
        coreCount.m_cpuLogicalCoreCount = logicalProcessorCount;

        return coreCount;
    }
コード例 #6
0
ファイル: 143C.cpp プロジェクト: saisrivatsan/Codeforces
/* Program to test function countSetBits */
int main()
{
    for(int i =0;i<32;i++)
        printf("%d %d\n",i,countSetBits(i));
    return 0;
}
コード例 #7
0
ファイル: count_bits.cpp プロジェクト: M4573R/Bit-Algorithms
int main() {
    int n = 8;
    printf("%d\n", countSetBits(n));
	return 0;
}